12

How to load application settings to NHibernate.Cfg.Configuration object by using System.Configuration.ConfigurationManager from App.config?

user366312
  • 16,949
  • 65
  • 235
  • 452

2 Answers2

25

The hibernate configuration can also be moved into app.config, which simplifies the startup code. See section XML Configuration File in the NHibernate reference manual.

Configuration cfg = new NHibernate.Cfg.Configuration();
ISessionFactory sf = cfg.Configure().BuildSessionFactory();

And in app.config:

<configuration>
        <configSections>
            <section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate" />
        </configSections>
        <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
            <session-factory>
                <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
                <property name="connection.dialect">NHibernate.Dialect.MsSql2005Dialect</property>
                <property name="connection.connection_string_name">Northwind</property>
                <property name="proxyfactory.factory_class">NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu</property>
                <mapping assembly="assemblyname" />
            </session-factory>
        </hibernate-configuration>
        <connectionStrings>
                <add name="Northwind" connectionString="Data Source=(local);Initial Catalog=Northwind;Trusted_Connection=True;>
        </connectionStrings>
</configuration>
Mwiza
  • 7,780
  • 3
  • 46
  • 42
Lachlan Roche
  • 25,678
  • 5
  • 79
  • 77
  • xmlns="urn:nhibernate-configuration-2.2" in the is very important. I didn't have it, and kept getting error. Thank you @Lachlan_Roche – Brij Jan 26 '18 at 10:01
21

app.config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <connectionStrings>
    <add name="Northwind" connectionString=
       "Data Source=(local);Initial Catalog=Northwind;Trusted_Connection=True;>
  </connectionStrings>
</configuration>

C# code:

string connectionString =  System.Configuration.ConfigurationManager
                                 .ConnectionStrings["Northwind"].ToString();

NHibernate.Cfg.Configuration nHibernateConfiguration =
                                      new NHibernate.Cfg.Configuration();
nHibernateConfiguration.SetProperty(
  NHibernate.Cfg.Environment.ProxyFactoryFactoryClass,
  typeof(NHibernate.ByteCode.Castle.ProxyFactoryFactory).AssemblyQualifiedName);
nHibernateConfiguration.SetProperty(
  NHibernate.Cfg.Environment.Dialect,
  typeof(NHibernate.Dialect.MsSql2005Dialect).AssemblyQualifiedName);
nHibernateConfiguration.SetProperty(
  NHibernate.Cfg.Environment.ConnectionString, connectionString);
nHibernateConfiguration.SetProperty(
  NHibernate.Cfg.Environment.FormatSql, "true");
nHibernateConfiguration.AddAssembly(Assembly.GetCallingAssembly());

ISessionFactory oneISessionFactory = nHibernateConfiguration
                                        .BuildSessionFactory();
Michael Maddox
  • 12,331
  • 5
  • 38
  • 40
  • Instead of doing the manual labor of getting the value from the configuration manager, try setting `connection_string_name`. See [How to configure NHibernate to use connection string from configuration section](http://stackoverflow.com/questions/455664/how-to-configure-nhibernate-to-use-connection-string-from-connectionstrings-co) and @LachlanRoche's answer. – Joel Purra Apr 05 '12 at 21:43
  • 2
    @Joel: Did you read the question before downvoting me? It specifically calls out System.Configuration.ConfigurationManager. Lachlan's answer is helpful, but it doesn't answer the question that was asked. – Michael Maddox Apr 06 '12 at 12:41
  • Considering that NHibernate uses `ConfigurationManager` too, its not very (cross-project) DRY to rewrite that same piece of functionality. – Joel Purra Apr 06 '12 at 18:13