如何将NHibernate配置类与Fluent NHibernate一起使用

时间:2009-11-19 12:23:37

标签: nhibernate fluent-nhibernate nhibernate-configuration

我希望在部署代码后能够灵活处理,因此我喜欢使用hibernate.cfg.xml文件来配置NHibernate。现在,我打算使用Fluent NHibernate来完成我的所有Class =>表映射。有没有办法可以使用旧的NHibernate Configuration类来配置Fluent NHibernate?

2 个答案:

答案 0 :(得分:2)

是的,如果您正在使用fluent configuration API Configure方法有一个重载,它需要一个现有的NHibernate Configuration实例,该实例可以从您的hibernate.cfg.xml构建。

答案 1 :(得分:0)

好吧,所以这显然是我的错。我尝试将NHibernate Configurtion对象传递给Fluently.Configure()方法,但是我的代码抛出了各种错误。问题出在NHibernate'Fluent-NHibernate'用户的版本上。我不知道代理工厂类属性现在是必需的。所以,我的hibernate.cfg.xml文件缺少该属性。这很奇怪,流利的NHibernate并没有给我任何线索。当我尝试使用普通的NHibernate时,我发现了这个问题。下面是我的hibernate.cfg.xml文件的不同版本。希望它能帮助未来的开发者。

第一版

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

第二版

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