问题
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