NHibernate App.Config
When you new Configuration().Configure(), app/web.config is easiest (and the first default). If not found, it looks for hibernate.cfg.xml in the current directory, or you must pass in a path or an IDictionary.
- Change the dialect and driver class to fit the database.
- Change the connection string (connection.connection_string_name for the standard connectionStrings section, or connection.connection_string)
- The current_session_context_class will be "thread_static" in test and windows apps, and "web" in asp. Optional depending if you use session context.
- Bytecode provider (i.e., the engine that creates the proxies):
- From 2.1 (March 2009)-3.2, add "proxyfactory.factory_class" with Castle or LinFu. For Castle, include NHibernate.ByteCode.Castle.dll in deployment (plus Castle.DynamicProxy2.dll and Castle.Core).
- From 3.2 (April 2011), Castle is built in (ILMerged) by default, but you can replace it if required.
- You'll probably want to configure log4net to show more detailed sql (but not everything). See here (add a logger for "NHibernate.SQL" on ALL)
<?xml version="1.0" encoding="utf-8" ?>
<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>
<!-- Change these to Oracle, Sql2000 etc-->
<property name="connection.driver_class">
NHibernate.Driver.SqlClientDriver
</property>
<property name="dialect">
NHibernate.Dialect.MsSql2005Dialect
</property>
<!-- Connection string or connection string name -->
<!--<property name="connection.connection_string">Server=.\SQLEXPRESS;Database=Northwind;Integrated Security=True;</property>-->
<property name="connection.connection_string_name">
Northwind
</property>
<!-- web or thread_static-->
<property name="current_session_context_class">
thread_static
</property>
<!-- Help debugging-->
<property name="show_sql">
true
</property>
<!-- NHibernate 2.1 - 3.2 -->
<property name="proxyfactory.factory_class">
NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle
</property>
</session-factory>
</hibernate-configuration>
<connectionStrings>
<add name="Northwind" connectionString="Server=.\SQLEXPRESS;Database=Northwind;Integrated Security=True;" providerName="System.Data.SqlClient"/>
</connectionStrings>
</configuration>
NHibernate with caching
Here's a version adding caching.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="hibernate-configuration"
type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate" />
<section name="syscache"
type="NHibernate.Caches.SysCache.SysCacheSectionHandler, NHibernate.Caches.SysCache" />
</configSections>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2" >
<session-factory>
<property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
<property name="dialect">NHibernate.Dialect.MsSql2008Dialect</property>
<property name="connection.connection_string_name">Northwind</property>
<property name="current_session_context_class">web</property>
<property name="show_sql">true</property>
<!-- use caching -->
<property name="cache.provider_class">NHibernate.Caches.SysCache.SysCacheProvider, NHibernate.Caches.SysCache</property>
<property name="cache.use_second_level_cache">true</property>
<property name="cache.use_query_cache">true</property>
</session-factory>
</hibernate-configuration>
<!-- SysCache -->
<syscache>
<cache region="Hour" expiration="3600" priority="2" />
<cache region="Minute" expiration="60" priority="3" />
</syscache>
<connectionStrings>
<add name="Northwind"
connectionString="Data Source=.\SQLEXPRESS;Integrated Security=true;Initial Catalog=Northwind"
providerName="System.Data.SqlClient"/>
</connectionStrings>
</configuration>
Versioning hell
If you use NHibernate with dependent products (often, Caches and/or FluentNHibernate), the dependencies may not be built against the current version. Normally a bindingRedirect will fix it up.
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="NHibernate" publicKeyToken="aa95f207798dfdb4" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-3.3.1.4000" newVersion="3.3.1.4000" />
</dependentAssembly>
</assemblyBinding>
</runtime>
Fluently configure
Fluent NHibernate allows you to configure in code. But keep your connection string in the config, please. This can live in, for example, you Global.asax or base test class.
public static ISessionFactory Configure()
{
return Fluently.Configure()
//which database
.Database(
MsSqlConfiguration.MsSql2008
//connection string from app.config
.ConnectionString(
cs => cs.FromConnectionStringWithKey("Northwind"))
.ShowSql())
//2nd level cache
.Cache(
c => c.UseQueryCache()
.UseSecondLevelCache()
.ProviderClass<NHibernate.Cache.HashtableCacheProvider>())
//find the mappings
.Mappings(m => m.FluentMappings.AddFromAssemblyOf<CategoryMapping>())
.BuildSessionFactory();
}