更新:现在可以在NuGet和GitHub 上使用
这是我一直想要的功能。它从来没有出现过,所以随着时间的推移,我创建了一个名为 NHibernate X-Factories 的扩展方法。您所要做的就是在一个 .cfg.xml 中创建多个会话工厂元素并命名它们。然后,您可以在配置 sessionFactory 时按名称调用它们。
休眠.cfg.xml
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2-x-factories">
<session-factory name="Development">
<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
<property name="dialect">NHibernate.Dialect.MsSql2008Dialect</property>
<property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
<property name="connection.connection_string">Server=dsql01;DataBase=dbDev;uid=nhDeveloper;pwd=pass1234</property>
<property name="show_sql">true</property>
<mapping assembly="DataLayer" />
</session-factory>
<session-factory name="Production">
<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
<property name="dialect">NHibernate.Dialect.MsSql2008Dialect</property>
<property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
<property name="connection.connection_string">Server=psql02;DataBase=dbDev;uid=nhDeveloper;pwd=pass5678</property>
<property name="show_sql">false</property>
<mapping assembly="DataLayer" />
</session-factory>
</hibernate-configuration>
C#
NHibernate.Cfg.Configuration config = new NHibernate.Cfg.Configuration();
config.Configure("~/nhibernate.cfg.xml", "Development").BuildSessionFactory();
在https://www.github.com/roydukkey/NHibernate-X-Factories/上查看更多信息。