NHibernate自定义连接字符串配置

Dar*_*der 2 .net c# database nhibernate ado.net

我有一个使用nhibernate配置的ac#库项目,并且我希望人们能够导入此项目并使用该项目。该项目的FrontController可以完成所有工作。

我在休眠配置文件和另一个项目的app.config文件中有一个连接字符串。

任何人都可以将连接字符串设置到此库项目中并使用它,将是一个很好的选择。例如通过将connectiong字符串作为参数的方法。或在创建FrontController的新实例以将连接字符串传递给构造函数时。或者您有更好的主意。

这该怎么做?

我希望该类库使用与导入的项目相同的数据库。

如何以编程方式设置休眠连接字符串?

log4net的想法相同。

Mau*_*fer 5

这取决于您如何构建NHibernate.Cfg.Configuration会话工厂。可以这样简单:

public ISessionFactory BuildSessionFactory(string connectionString) {
    var cfg = new Configuration()
                .AddProperties(new Dictionary<string, string> {
                    {Environment.ConnectionDriver, typeof (SQLite20Driver).FullName},
                    {Environment.ProxyFactoryFactoryClass, typeof (ProxyFactoryFactory).AssemblyQualifiedName},
                    {Environment.Dialect, typeof (SQLiteDialect).FullName},
                    {Environment.ConnectionProvider, typeof (DriverConnectionProvider).FullName},
                    {Environment.ConnectionString, connectionString},
                })
                .AddAssembly(Assembly.GetExecutingAssembly());
    return cfg.BuildSessionFactory();
}
Run Code Online (Sandbox Code Playgroud)

我不确定您想对log4net做什么。