2016-07-27 72 views
0

我使用.net 4.5与NHibernate 3.3.3.4000,我想配置一个nhibernate.config与2会话工厂。 我试图configurate我nhibernate.config这样:与x会话工厂nhibernate配置

<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的“会话工厂”是无效的:出现 异常解析配置:在命名空间中的元素“休眠配置” 'urn:nhibernate-configuration-2.2'在命名空间'urn:nhibernate-configuration-2.2'中有无效的子元素'session-factory'。

你知道我如何配置2会话工厂的文件?

问候 菲尔

+0

你应该使用NH4和.Net 4.5。 –

+0

这是否真的是您在异常中获得的唯一信息,以及它的所有InnerExceptions(如果有)? –

+0

也许这篇文章可以帮助你:http://www.codeproject.com/Articles/14846/Using-NHibernate-with-Multiple-Databases –

回答

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

+0

非常感谢。这看起来不错 – Phil