开发者

How can I use the NHibernate Configuration class with Fluent NHibernate

开发者 https://www.devze.com 2022-12-12 12:23 出处:网络
I want to be flexible even after deploying my code so I like to use the hibe开发者_运维技巧rnate.cfg.xml file for configuring NHibernate. Now, I am planning to use Fluent NHibernate to do all my Class

I want to be flexible even after deploying my code so I like to use the hibe开发者_运维技巧rnate.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?


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.


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>
0

精彩评论

暂无评论...
验证码 换一张
取 消