views:

433

answers:

2

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.

+2  A: 

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);
  }
}
Nick Craver
Thank you, but I need to get this on my SessionFactory getter. I want the NHibernate connection string to create my SessionFactory. I'm using Fluent NHibernate.
MCardinale
@MCardinale - The factory should be using this connection string natively, you need to change it or...?
Nick Craver
I have edited my question, adding my SessionFactory's code. I want use the connection string from NHibernate instead of Web.config or a hard coded string because this Factory is on a Class Library to reuse in other projects. Thank you very much.
MCardinale
@MCardinale - Updated the answer, you can use this as a property on whatever you like to fetch the string.
Nick Craver
if you put this as-is it is going to rebuild the configuration each and everytime you try to get the ConnectionString
Jaguar
@Jaguar - We cached this in a central spot before moving away, but you're right without that not a great idea...been a while since we moved away from nHibernate.
Nick Craver
+2  A: 

try this

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