简体   繁体   English

NHibernate自定义连接字符串配置

[英]NHibernate custom connection string configuration

I have ac# library project, that i configured using nhibernate, and I like people to be able to import this project and use the project. 我有一个使用nhibernate配置的ac#库项目,并且我希望人们能够导入此项目并使用该项目。 This project has FrontController that does all the work. 该项目的FrontController可以完成所有工作。

I have a connection string in hibernate config file and in app.config file of another project. 我在休眠配置文件和另一个项目的app.config文件中有一个连接字符串。

it would be nice for anyone to be able to set the connection string into this library project and use it. 任何人都可以将连接字符串设置到此库项目中并使用它,将是一个很好的选择。 such as through a method which will take the connectiong string as parameter. 例如通过将connectiong字符串作为参数的方法。 or when creating a new instance of FrontController to pass the connection string to constructor. 或在创建FrontController的新实例以将连接字符串传递给构造函数时。 or if you have a better idea. 或者您有更好的主意。

How to do this? 这个怎么做?

I d like this class library to use the same database of the project that s imported. 我希望该类库使用与导入的项目相同的数据库。

How to set hibernate connection string programatically? 如何以编程方式设置休眠连接字符串?

same idea for log4net. log4net的想法相同。

It depends on how you build your NHibernate.Cfg.Configuration and session factory. 这取决于您如何构建NHibernate.Cfg.Configuration和会话工厂。 It can be as simple as this: 可以这样简单:

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

I'm not sure what you want to do with log4net though. 我不确定您想对log4net做什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM