How can I use the NHibernate Configuration class with Fluent NHibernate

1.5k views Asked by At

I want to be flexible even after deploying my code so I like to use the hibernate.cfg.xml file for configuring NHibernate. Now, I am planning to use Fluent NHibernate to do all my Class => Table mapping. Is there a way I could use the old NHibernate Configuration class to configure Fluent NHibernate?

2

There are 2 answers

0
James Gregory On BEST ANSWER

Yes, if you're using the fluent configuration API the Configure method has an overload that takes an existing NHibernate Configuration instance, which can be built from your hibernate.cfg.xml.

0
Khaja Minhajuddin On

Alright, So this was obviously my fault. I tried passing the NHibernate Configurtion object into the Fluently.Configure() method, but my code was throwing up all kinds of errors. The problem was with the version of NHibernate 'Fluent-NHibernate' users. I didn't know that the proxy factory class attribute was now mandatory. So, my hibernate.cfg.xml file was missing that attribute. It's wierd, Fluent NHibernate didn't give me any clue about that. It's when I tried using plain NHibernate that I found this problem. Below are the different version of my hibernate.cfg.xml files. Hope it helps future devs.

First Version

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
  <session-factory>
    <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
    <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
    <property name="dialect">NHibernate.Dialect.MsSql2005Dialect,NHibernate</property>
    <property name="connection.connection_string">Data Source=.\SQLEXPRESS;Initial Catalog=SchoolPilot;Integrated Security=True</property>
    <property name="show_sql">true</property>
  </session-factory>
</hibernate-configuration>

Second Version

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
  <session-factory>
    <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
    <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
    <property name="dialect">NHibernate.Dialect.MsSql2005Dialect,NHibernate</property>
    <property name="proxyfactory.factory_class">NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle</property>
    <property name="connection.connection_string">Data Source=.\SQLEXPRESS;Initial Catalog=SchoolPilot;Integrated Security=True</property>
    <property name="show_sql">true</property>
  </session-factory>
</hibernate-configuration>