nhibernateconnection-stringhibernate.cfg.xml

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


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=...' />

Solution

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