2

My client asked the posibility to switch from a c# app from the "CURRENT" database to a TEST database or DEV database. Only one can be active at a time. In a menu file she choose DEV or TEST database.

How do I configure 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=...' />
Community
  • 1
  • 1
Dan Nick
  • 132
  • 1
  • 2
  • 12

2 Answers2

5

Update: This is now available on NuGet and GitHub

This is a feature I've wanted for sometime. It never came, so over time I've created an extension method called NHibernate X-Factories. All you have to do is create multiple session-factory elements in one .cfg.xml and name them. Then you're able to call them by name when you configure the 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>

C#

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

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

roydukkey
  • 3,149
  • 2
  • 27
  • 43
  • 1
    I have chosen this solution: var connectionString = DecryptConnectionString("filename.xml"); var cfg = new Configuration(); cfg.Configure(); cfg.SetProperty("connection.connection_string",connectionString); cfg.BuildSessionFactory(); // in filename.xml I stored ; – Dan Nick May 29 '12 at 08:39
0

I can think of two ways around this:

One is to place NHibernate configuration outside *.config file, then listening to this external file using FileSystemWatcher class. You can change the file content dynamically.

Another way is to have two NHibernate Configuration objects at the same time, and then injecting it's corresponding ISession to your DAO / UnityOfWork / whatever

If this is just for dev / test / production database, I recommend against both. It's better to have three different environments, each with it's own *.config file .

Ortiga
  • 8,455
  • 5
  • 42
  • 71
  • I don't need to change the database at runtime. The user must logout after database name change and then login using the last selected database name. I need 3 connection strings which must be encrypted. How do I tell NH that I have 3 connection strings and choose one by name. I thought there is a way to store 3 connection strings in hibernate.cfg.xml and choose at runtime by the name. – Dan Nick May 04 '12 at 14:18
  • Use first option, then. You can have three different files, and choose between them passing the file name. The content of the files can be encrypted, in which case you can decrypt and configure NHibernate using `nhconfig.AddXmlString()`. – Ortiga May 04 '12 at 14:29