开发者

How to get the connection string value from hibernate.cfg.xml file?

开发者 https://www.devze.com 2022-12-22 22:31 出处:网络
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:

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

精彩评论

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