How to get the connection string value from hibernate.cfg.xml file?
I'm using Fluent NHibernate and need to get my Connection String from the connection.connection_string property on hibernate.cfg.xml file to create my Session Factory:
private static ISessionFactory SessionFactory {
get {
return = Fluently.Configure()
.Database(MySQLConfiguration.Standard.ConnectionString(c => c.FromConnectionStringWithKey("MyConnStr")))
.Mappings(m => m.FluentMappings.AddFromAssemblyOf<FooMap>())
.ExposeConfiguration(c => c.P开发者_如何学JAVAroperties.Add("hbm2ddl.keywords", "none"))
.BuildSessionFactory();
}
}
I want to replace MyConnStr (that is in my web.config file) "c => c.FromConnectionStringWithKey("MyConnStr")" for the connection string from the hibernate.cfg.xml file.
I've tried use NHibernate.Cfg.Environment.ConnectionString, but it didn't work.
How can I get this?
Thank you.
try this
NHibernate.Cfg.Configuration cfg = new NHibernate.Cfg.Configuration().Configure();
string conString = cfg.Configuration.GetProperty(NHibernate.Cfg.Environment.ConnectionString);
Updated for your updated question
public static string ConnectionString
{
get
{
NHibernate.Cfg.Configuration cfg = new NHibernate.Cfg.Configuration();
return cfg.GetProperty(NHibernate.Cfg.Environment.ConnectionString);
}
}
精彩评论