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

I am using Fluent NHibernate and should get my connection string from the connection.connection_string property in the hibernate.cfg.xml file to create my Factory session

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.Properties.Add("hbm2ddl.keywords", "none")) .BuildSessionFactory(); } } 

I want to replace MyConnStr (this is in my web.config file) " c => c.FromConnectionStringWithKey (" MyConnStr ") " for the connection string from hibernate.cfg.xml .

I tried using NHibernate.Cfg.Environment.ConnectionString, but this did not work.

How can i get this?

Thanks.

+4
source share
2 answers

try it

 NHibernate.Cfg.Configuration cfg = new NHibernate.Cfg.Configuration().Configure(); string conString = cfg.Configuration.GetProperty(NHibernate.Cfg.Environment.ConnectionString); 
+10
source

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); } } 
+4
source

Source: https://habr.com/ru/post/1303932/


All Articles