Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将NHibernate hibernate.cfg.xml文件配置为具有更多连接字符串_Nhibernate_Connection String_Hibernate.cfg.xml - Fatal编程技术网

将NHibernate hibernate.cfg.xml文件配置为具有更多连接字符串

将NHibernate hibernate.cfg.xml文件配置为具有更多连接字符串,nhibernate,connection-string,hibernate.cfg.xml,Nhibernate,Connection String,Hibernate.cfg.xml,我的客户要求将c#app从“当前”数据库切换到测试数据库或开发数据库。 一次只能有一个处于活动状态。在菜单文件中,她选择DEV或testdatabase 如何将hibernate.cfg.xml文件配置为具有更多连接字符串,如app.config 我可以想出两种解决方法: 一种是将NHibernate配置放在*.config文件之外,然后使用FileSystemWatcher类侦听这个外部文件。您可以动态更改文件内容 另一种方法是同时拥有两个NHibernate配置对象,然后将其对应的ISess

我的客户要求将c#app从“当前”数据库切换到测试数据库或开发数据库。 一次只能有一个处于活动状态。在菜单文件中,她选择DEV或testdatabase

如何将hibernate.cfg.xml文件配置为具有更多连接字符串,如app.config


我可以想出两种解决方法:

一种是将NHibernate配置放在*.config文件之外,然后使用FileSystemWatcher类侦听这个外部文件。您可以动态更改文件内容

另一种方法是同时拥有两个NHibernate配置对象,然后将其对应的ISession注入DAO/UnityOfWork/which

如果这只是针对dev/test/production数据库,我建议两者都不要使用。最好有三个不同的环境,每个环境都有自己的*.config文件。

更新:现在可以在和上使用

这是我一直想要的功能。它从未出现过,所以随着时间的推移,我创建了一个名为NHibernate X-Factorys的扩展方法。您所要做的就是在一个.cfg.xml中创建多个会话工厂元素并命名它们。然后,您可以在配置sessionFactory时按名称调用它们

nhibernate.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>

有关详细信息,请参见。

我不需要在运行时更改数据库。用户必须在数据库名称更改后注销,然后使用最后选择的数据库名称登录。我需要3个必须加密的连接字符串。如何告诉NH我有3个连接字符串并按名称选择一个。我认为有一种方法可以在hibernate.cfg.xml中存储3个连接字符串,并在运行时根据名称进行选择。您可以有三个不同的文件,并在它们之间选择传递文件名。文件的内容可以加密,在这种情况下,您可以使用
nhconfig.AddXmlString()
对NHibernate进行解密和配置;var cfg=新配置();Configure();SetProperty(“connection.connection_string”,connectionString);cfg.BuildSessionFactory();//存储在filename.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>
NHibernate.Cfg.Configuration config = new NHibernate.Cfg.Configuration();
config.Configure("~/nhibernate.cfg.xml", "Development").BuildSessionFactory();