O'Reilly logo

NHibernate 3.0 Cookbook by Jason Dentler

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Using dynamic connection strings

There are cases where an application may need to change connection strings depending on some condition. This can be in the context of a multi-tenant application, or perhaps a database failover scenario. In this recipe, I'll show you how to switch NHibernate connection strings at runtime.

How to do it...

  1. Start a new console application project named DynamicConnectionString.
  2. Add references to NHibernate.dll, NHibernate.ByteCode.Castle.dll, log4net.dll, and the Eg.Core model from Chapter 1.
  3. Add a reference to System.Configuration from the .NET framework.
  4. Set up the App.config with a standard NHibernate and log4net configuration.
  5. Add the following DynamicConnectionProvider class:
    public class DynamicConnectionProvider : DriverConnectionProvider ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required