How to set connection string dynamically in NHibernate

connectionnhibernatestring

I want assign connection string for NHibernate using following code but it throw an exception.

log4net.Config.DOMConfigurator.Configure();
Configuration config = new Configuration();
IDictionary props = new Hashtable();

props["hibernate.connection.provider"] = "NHibernate.Connection.DriverConnectionProvider";
props["hibernate.dialect"] = "NHibernate.Dialect.MsSql2000Dialect";
props["hibernate.connection.driver_class"] = "NHibernate.Driver.SqlClientDriver";
props["hibernate.connection.connection_string"] = @"Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=Sample;Data Source=HYDHTC92318D\SQLEXPRESS";
props["hibernate.connection.current_session_context_class"] = "web";
props["hibernate.connection.show_sql"] = "true";

props["hibernate.connection.proxyfactoryfactory.factory_class"] = "NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle";

foreach (DictionaryEntry de in props)
{
  config.SetProperty(de.Key.ToString(), de.Value.ToString());
}

config.AddAssembly("nhibernator");
factory = config.BuildSessionFactory();
session = factory.OpenSession();

Exception:

The ProxyFactoryFactory was not configured.
Initialize 'proxyfactory.factory_class' property of the session-factory configuration section with one of the available NHibernate.ByteCode providers.
Example:
NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu
Example:
NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle**

Please let me know the solution.

Best Answer

See here for a solution:

How to load application settings to NHibernate.Cfg.Configuration object?

I haven't tested this, but you may want to try changing:

props["hibernate.connection.proxyfactoryfactory.factory_class"] =
  "NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle";

to:

props["proxyfactory.factory_class"] =
  "NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle";

The proxyfactory isn't part of the connection.