开发者

NHibernate custom connection string configuration

开发者 https://www.devze.com 2022-12-30 14:32 出处:网络
I have a c# library project, that i configured using nhibernate, and I like people to be able to import this project and use the project. This project has FrontController that does all the work.

I have a c# library project, that i configured using nhibernate, and I like people to be able to import this project and use the project. This project has FrontController that does all the work.

I have a connection string in hibernate config file and in app.config file of another projec开发者_如何学Got.

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. or when creating a new instance of FrontController to pass the connection string to constructor. 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.


It depends on how you build your NHibernate.Cfg.Configuration and session factory. 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消