Using NHibernate, Sqlite in your ASP.Net MVC project

1 minute read

I used Entity Framework for my personal projects for a while but wanted to try NHibernate. Instead of NUnit, I tried MS Test this time.

First problem was that MS Test fails, complaining that hibernate.cfg.xml does not exist. It did not copy the file to MS Test out directory. Of course, I set "Copy to Output Directory" to "Copy always". Thanks to NHibernate Getting Started Guide. It is copied to bin folder, but not to output folder. "System.IO.FileNotFoundException: Could not find file '...\TestResults\684 2010-04-29 09_42_19\Out\hibernate.cfg.xml'..  " occurred each time I ran the test. Some say it is a bug in MS Test, and I agree.

In order to copy the file to the output, you need to use DeploymentItem attribute. Thanks to Stackoverflow post, "NHibernate and MS Test".You can put the attribute at class level as well as method level. I put it on the test class.

[sourcecode language="csharp"]

[TestClass, DeploymentItem(@".\hibernate.cfg.xml")]
public class Task_Test


Then I had this error message; "Could not create the driver from NHibernate.Driver.SQLiteDriver"
I use "System.Data.SQLite" from a little bit of googling, I found out that the example configuration was using "NHibernate.Driver.SQLiteDriver" and it is for old Sqlite driver.
The connection.driver_class should be "NHibernate.Driver.SQLite20Driver" in order to use System.Data.SQLite.
The below is my config.


[sourcecode language="xml"]

<?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.SQLiteDialect</property>
<property name="connection.driver_class">NHibernate.Driver.SQLite20Driver</property>
<property name="connection.connection_string">data source=SprintManager.sqlite</property>
<property name="show_sql">true</property>
<property name="proxyfactory.factory_class">NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu</property>