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.
hibernate.connection.datasource. It represents datasource JNDI name which is used by Hibernate for database properties. hibernate.jndi.url. It is optional. It represents the URL of the JNDI provider.
Hibernate Configuration File(cfg file) is the file loaded into an hibernate application when working with hibernate. Hibernate uses this file to establish connection to the database server.It is an XML file which is used to define below information. Standard name for this file is hibernate.
This is the root element having <session-factory> is a child element. <session-factory> contains database and mapping information. Way to register or load the driver. We provide fully qualified class name so that hibernate will figure out which class need to be load.
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);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With