NHibernate using single configuration file to connect to multiple dbs

孤街醉人 提交于 2020-01-14 02:22:08

问题


I'd like to have a single configuration file and then when I'm creating a session change the hibernate-configuration->session-factory->connection.connection_string property to what I want it to be programmatically? Is it possible?

UPDATE:

I believe I may be able to do this like this

Configuration cfg = new Configuration();
cfg.Configure(sessionFactoryConfigPath);

cfg.Properties["connection.connection_string"] = ...

What I would wonder than, if that is ok, Is this a bad way to handle connecting to a different database for each session? if so why etc. I'd also like to know if/how you can open an nhibernate session with a .net connection object?


回答1:


Use the ISessionFactory.OpenSession() overload that takes a IDbConnection.

That's how Castle's DifferentDatabaseScope does it.




回答2:


I think it is better to use different configuration files per each data base just becouse you will be able to switch dialects very esasy.




回答3:


You may also see this answer as it allows full configuration of session factories through one file. Configure NHibernate hibernate.cfg.xml file to have more connection strings



来源:https://stackoverflow.com/questions/1471146/nhibernate-using-single-configuration-file-to-connect-to-multiple-dbs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!