Using NHibernate, Sqlite in your ASP.Net MVC project

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.


[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 sqlite.phxsoftware.com.After 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.

hibernate.cfg.xml


<?xml version="1.0" encoding="utf-8" ?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
 <session-factory>
 <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>

 </session-factory>
</hibernate-configuration>
Using NHibernate, Sqlite in your ASP.Net MVC project

4 thoughts on “Using NHibernate, Sqlite in your ASP.Net MVC project

  1. Hey man have any good resources for getting NHibernate to work with ASP.Net MVC 2.0? From what I have read it sounds like I need to create a front controller for the Nhibernate session but I am not sure how to do that.

    Thanks

    Jackie

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s