使用x会话工厂的nhibernate配置

时间:2016-07-27 07:27:59

标签: c# .net nhibernate orm

我正在使用带有NHibernate 3.3.3.4000的.net 4.5,我想用2个会话工厂配置一个nhibernate.config。 我试着以这种方式配置我的nhibernate.config:

<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
  <session-factory name="Configuration">
    <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
    <property name="connection.driver_class">NHibernate.Driver.SQLite20Driver</property>
    <property name="connection.connection_string">Data Source=Configuration.db;Version=3</property>
    <property name="dialect">NHibernate.Dialect.SQLiteDialect</property>
    <property name="query.substitutions">true=1;false=0</property>
    <property name="show_sql">false</property>
    <mapping assembly="DataModel"/>
  </session-factory>
  <session-factory name="Texts">
    <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
    <property name="connection.driver_class">NHibernate.Driver.SQLite20Driver</property>
    <property name="connection.connection_string">Data Source=Texts.db;Version=3</property>
    <property name="dialect">NHibernate.Dialect.SQLiteDialect</property>
    <property name="query.substitutions">true=1;false=0</property>
    <property name="show_sql">false</property>
    <mapping assembly="DataModel_Texts"/>
  </session-factory>
</hibernate-configuration>

但是我得到了一个HibernateConfigException,即&#34; session-factory&#34;是无效的: 解析配置时出现异常:元素&#39; hibernate-configuration&#39; in namespace&#39; urn:nhibernate-configuration-2.2&#39;有无效的子元素&#39; session-factory&#39;在命名空间&#39; urn:nhibernate-configuration-2.2&#39;。

您是否了解如何使用2个会话工厂配置文件?

最好的问候 菲尔

1 个答案:

答案 0 :(得分:0)

您可以尝试NHibernate X-Factories,它为NHibernate配置方法提供扩展方法。扩展方法允许指定名称以从配置文件中选择适当的会话工厂。

设置很少,但您的配置文件看起来像这样:

<hibernate-configuration-x-factories xmlns="urn:nhibernate-configuration-2.2-x-factories">
  <session-factory name="Configuration">
    <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
    <property name="connection.driver_class">NHibernate.Driver.SQLite20Driver</property>
    <property name="connection.connection_string">Data Source=Configuration.db;Version=3</property>
    <property name="dialect">NHibernate.Dialect.SQLiteDialect</property>
    <property name="query.substitutions">true=1;false=0</property>
    <property name="show_sql">false</property>
    <mapping assembly="DataModel"/>
  </session-factory>
  <session-factory name="Texts">
    <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
    <property name="connection.driver_class">NHibernate.Driver.SQLite20Driver</property>
    <property name="connection.connection_string">Data Source=Texts.db;Version=3</property>
    <property name="dialect">NHibernate.Dialect.SQLiteDialect</property>
    <property name="query.substitutions">true=1;false=0</property>
    <property name="show_sql">false</property>
    <mapping assembly="DataModel_Texts"/>
  </session-factory>
</hibernate-configuration-x-factories>

可以通过以下方式配置会话工厂:

NHibernate.Cfg.Configuration config = new NHibernate.Cfg.Configuration();

config
    .Configure(HostingEnvironment.MapPath("~/nhibernate.cfg.xml"), "Development")
    .BuildSessionFactory();

可以在github repo上找到文档。