I am trying to programmatically set the connection string, as I am in a multi-company, multi-database scenario. I found this article on doing so in Java, but I am not having any luck with NHibernate.
http://www.hibernate.org/386.html
Here is my App.config file:
<nhibernate>
<add key="hibernate.connection.driver_class" value="NHibernate.Driver.SqlClientDriver" />
<add key="hibernate.connection.isolation" value="ReadCommitted" />
<add key="hibernate.connection.provider" value="NHibernate.Connection.DriverConnectionProvider" />
<add key="hibernate.dialect" value="NHibernate.Dialect.MsSql2000Dialect" />
</nhibernate>
Here is my sample code:
// Specify connection string.
string dsn = "connection string goes here";
// Initialize NHibernate Session Factory.
Configuration cfg = new Configuration();
cfg.AddAssembly("AssemblyName");
cfg.SetProperty("hibernate.connection.connection_string", dsn);
ISessionFactory factory = cfg.BuildSessionFactory();
// Open a session.
ISession session = factory.OpenSession();
The last line throws this error: Could not find connection string setting.
If I do specify the key/value pair for the connection string in the App.config, I get this error: cannot open connection.
So my code is still trying to load the connection string from the App.config file, even though I have tried to override this value programmatically with the Configuration object.
I must be using the wrong objects, doing things in the wrong order, or I'm just plain way off track.
I have extracted the threading code and other infrastructure out of this example, and I don't believe that additional code is the problem.
Does anyone have any idea on how to approach this? Any help would be much appreciated.
Thanks,
Michael