Tim Van Wassenhove

Passionate geek, interested in Technology. Proud father of two

28 Jul 2008

NHibernate SessionFactory configuration

My preferred way for configuring NHibernate is as following

  • Create a hibernate.cfg.xml file for session-factory settings.
  • Add mapping files, named type.hbm.xml, as embedded resources to the library project that implements the repository.

When i looked at the documentation i found the following

Another alternative (probably the best) way is to let NHibernate load all of the mapping files contained in an Assembly:

Configuration cfg = new Configuration(); 
cfg.AddAssembly( "NHibernate.Auction" );

Whenever i tried that code i received an InvalidOperationException: Could not find the dialect in the configuration. In order to make the configuration work the way i prefer you have to do the following

Configuration cfg = new Configuration().Configure();

Another attention point is the fact that i believe that all connectionStrings belong in App.Config. This can be realised by using the connection.connection_string_name attribute

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
		<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
		<property name="dialect">NHibernate.Dialect.MsSql2005Dialect</property> 
		<property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property> 
		<property name="connection.connection_string_name">AdventureWorks</property>