CHAPTER 2
Before using NHibernate, you must configure it. Basic required configuration consists of the database driver, the dialect to use, and the driver-specific database connection string. As you will see, there are several ways to make this configuration in NHibernate.
For the drivers and dialects, these are the ones included with NHibernate:
NHibernate Drivers and Dialects
Driver | Dialects | Description |
CsharpSqliteDriver | GenericDialect | A NHibernate driver for the Community CsharpSqlite data provider |
DB2400Driver | DB2400Dialect | A NHibernate driver for using the IBM.Data.DB2.iSeries data provider |
DB2Driver | DB2Dialect | A NHibernate driver for using the IBM.Data.DB2 data provider |
FirebirdClientDriver | FirebirdDialect | A NHibernate driver for using the Firebird data provider located in FirebirdSql.Data.FirebirdClient assembly |
FirebirdDriver | FirebirdDialect | A NHibernate driver for using the FirebirdSql.Data.Firebird data provider |
IfxDriver | InformixDialect InformixDialect0940 InformixDialect1000 | A NHibernate driver for using the Informix data provider |
IngresDriver | IngresDialect | A NHibernate driver for using the Ingres data provider |
MySqlDataDriver | MySQLDialect MySQL5Dialect | Provides a database driver for MySQL |
NpgsqlDriver | PostgreSQLDialect PostgreSQL81Dialect PostgreSQL82Dialect | The PostgreSQL data provider provides a database driver for PostgreSQL |
OdbcDriver | GenericDialect | A NHibernate driver for using the ODBC Data Provider |
OleDbDriver | GenericDialect | A NHibernate driver for using the OleDb Data Provider |
OracleClientDriver | Oracle8iDialect Oracle9iDialect Oracle10gDialect | A NHibernate driver for using the Oracle Data Provider |
OracleDataClientDriver | Oracle8iDialect Oracle9iDialect Oracle10gDialect | A NHibernate driver for using the Oracle.DataAccess data provider |
OracleLiteDataClientDriver | OracleLiteDialect | A NHibernate driver for using the Oracle.DataAccess.Lite data provider |
Sql2008ClientDriver | MsSql2008Dialect MsSqlAzure2008Dialect | SQL Server 2008 and Azure driver |
SqlClientDriver | MsSql7Dialect MsSql2000Dialect MsSql2005Dialect | A NHibernate driver for using the SqlClient data provider |
SQLite20Driver | SQLiteDialect | NHibernate driver for the System.Data.SQLite data provider for .NET 2.0 |
SqlServerCeDriver | MsSqlCeDialect MsSqlCe40Dialect | A NHibernate driver for Microsoft SQL Server CE data provider |
SybaseAsaClientDriver | SybaseASA9Dialect | The SybaseAsaClientDriver driver provides a database driver for Adaptive Server Anywhere 9.0 |
SybaseAseClientDriver | SybaseASE15Dialect | This provides a driver for Sybase ASE 15 using the ADO.NET driver |
SybaseSQLAnywhereDotNet4Driver | SybaseSQLAnywhere10Dialect | SQL Dialect for SQL Anywhere 12 |
SybaseSQLAnywhereDriver | SybaseSQLAnywhere10Dialect SybaseSQLAnywhere11Dialect | The SybaseSQLAnywhereDriver Driver provides a database driver for Sybase SQL Anywhere 10 and above |
Some of these drivers and dialects are subclasses of others, which means they inherit something from their ancestors as well as add something new. You are free to create your own classes by inheriting from the most appropriate one.
A driver is the class responsible by actually creating the connection, by instancing a System.Data.Common.DbConnection-derived class suitable for a particular database engine. If you have ODBC or OLE DB drivers available, you can connect to virtually any database by using the corresponding NHibernate drivers (but you will have better performance if you use a specific driver).
The dialect describes characteristics and registers functions of a specific version of an engine so, generally speaking, you should choose the dialect that is closer to the actual version you are using (although you can always use the generic dialect).
A configuration is, essentially, an instance of the NHibernate.Cfg.Configuration class. It needs to get populated somehow and is required for using NHibernate since everything starts from it. You may have several configuration instances, which is typical if you need to support different databases at the same time (even hosted in different engines). In most cases, however, you will need only one configuration instance.
In previous versions of NHibernate, the only possible configuration was through XML. The process now is: you register a custom section on the configuration file (App.config or Web.config) and you create this section, filling its required definitions. This section would look like this:
<?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="hbm2ddl.keywords">auto-quote</property> <property name="connection.driver_class">NHibernate.Driver.Sql2008ClientDriver </property> <property name="dialect">NHibernate.Dialect.MsSql2008Dialect</property> <property name="connection.connection_string_name">Succinctly</property> <property name="query.substitutions">true 1, false 0</property> <mapping assembly="Succinctly.Model" /> </session-factory> </hibernate-configuration> <connectionStrings> <add name="Succinctly" providerName="System.Data.SqlClient" connectionString="Data Source=. \sqlexpress; Integrated Security=SSPI; Initial Catalog=Succinctly"/> </connectionStrings> </configuration> |
This is a minimal NHibernate configuration. You can see that inside the hibernate-configuration section we are setting some properties and adding a mapping reference. Their meaning is:
To build the configuration instance and load settings from the configuration file, all you need is:
Configuration cfg = new Configuration().Configure(); |
Tip: Import the NHibernate.Cfg namespace.
If you would like to use XML-based configuration, one thing that may come in handy is Visual Studio IntelliSense. You can add such support by following the following steps:


NHibernate 3.2 brought along what is called loquacious (or fluent) configuration. Basically, it renders the configuration section on the configuration file unnecessary and, instead, relies on code to initialize NHibernate. The configuration expressed in the previous section can be transformed into the following code:
Tip: You will need to import the NHibernate, NHibernate.Cfg, NHibernate.Dialect, and NHibernate.Driver namespaces.
Some things worth mentioning:
Again, which one you choose is up to you. They are fundamentally equivalent. XML-based configuration offers you the advantage of allowing changes without requiring recompilation of the code. However, it merely delays eventual configuration errors to a later stage when the application is run. Loquacious configuration, on the other hand, may detect syntactic errors sooner—even preventing the code from compiling altogether—at the cost of requiring recompilation just to make even the smallest change. But it allows conditional configuration, something you can’t achieve with static XML. You can have the best of both worlds and have both XML-based as well as by code configuration in the same project. It may make sense if, for example, you have to deal with legacy HBM.XML files.
These are the most basic settings required for NHibernate to work. Most of this book’s examples are agnostic regarding which database engine is used. Whenever this is not the case, it will be explicitly mentioned. Later on we will learn functionality that will require additional settings and so we will come back to this.
Note: It is also possible to have the configuration entries in an external file; that is, not as content inside the App.config or Web.config files or even as a resource embedded in some assembly. These are not typical scenarios and won’t be covered here, but you are free to explore them by looking at the various overloads of the Configuration.Configure method.