Configure NHibernate hibernate.cfg.xml to have more connection strings

My client asked about the possibility of switching from a C # application from the "CURRENT" database to the TEST database or DEV database. Only one can be active at a time. In the menu file, she selects the DEV or TEST database.

How to set up hibernate.cfg.xml file to have more connection strings like app.config.

<name="CURRENT" connectionString='User Id=u1;Data Source=CURRENT;Password=...' />
<name="DEV" connectionString='User Id=u1;Data Source=DEV;Password=...' />
<name="TEST" connectionString='User Id=u1;Data Source=TEST;Password=...' />

+2
source share
2 answers

I can think of it in two ways:

- NHibernate- *.config, FileSystemWatcher. .

, NHibernate, ISession DAO/UnityOfWork/whatever

dev/test/production, . , *.config.

0

: NuGet GitHub

, - . , , NHibernate X-Factories. , , session- factory .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>

#

NHibernate.Cfg.Configuration config = new NHibernate.Cfg.Configuration();
config.Configure("~/nhibernate.cfg.xml", "Development").BuildSessionFactory();

https://www.github.com/roydukkey/NHibernate-X-Factories/.

+5

Source: https://habr.com/ru/post/1708914/


All Articles