如何在NHibernate中动态设置连接字符串

时间:2010-05-28 09:12:21

标签: nhibernate string connection

我希望使用以下代码为NHibernate分配连接字符串并获取异常(粗体)。

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();

未配置ProxyFactoryFactory。 使用一个可用的NHibernate.ByteCode提供程序初始化session-factory配置节的'proxyfactory.factory_class'属性。 例: NHibernate.ByteCode.LinFu.ProxyFactoryFactory,NHibernate.ByteCode.LinFu 例: NHibernate.ByteCode.Castle.ProxyFactoryFactory,NHibernate.ByteCode.Castle

请让我知道解决方案。 问候 JCReddy

1 个答案:

答案 0 :(得分:0)

请点击此处查看解决方案:

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

我没有对此进行测试,但您可能想尝试更改:

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

为:

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

proxyfactory不是连接的一部分。