MCardinale
MCardinale

Reputation: 1406

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.Properties.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.

Upvotes: 5

Views: 8485

Answers (2)

Jaguar
Jaguar

Reputation: 5958

try this

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

Upvotes: 12

Nick Craver
Nick Craver

Reputation: 630429

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

Upvotes: 4

Related Questions