1. Create a new MVC 3 project
First download and install MVC3 RC [ASP.NET MVC 3 RC]. ASP.NET MVC 3 can be installed side by side with ASP.NET MVC2 so MVC 2 doesn't need to be uninstalled for v3 to run. After installing v 3.0 create a new ASP.NET MVC 3 project. For this tutorial I will be using the Razor View Engine.
2. Download Nhibernate 3.0
3. Setup Fluent Nhibernate to Use LINQ
- Run the following command to get the latest Fluent Nhibernate source – assuming git is in your PATH. git clone git://github.com/jagregory/fluent-nhibernate.git fluent-nhibernate Add the Fluent Nhibernate VS.NET project located in [Path]\fluent-nhibernate\src\FluentNHibernate\ to your ASP.NET MVC 3 project.
- Convert the Fluent Nhibernate project to target
- Remove all Nhibernate 2.0 library references from Fluent NHibernate project. In particular you might be interested in the follwoing libraries – NHibernate, Iesi.Collections, Antlr3.Runtime.
- Add the NHibernate v3 libraries – all libraries removed in earlier step – to the project and build it.
4. Use NHibernate 3 LINQ
NHibernate defines the LINQ specific functionalities in NHibernate.Linq. Here is a sample code that shows how to use LINQ in NHibernate.
using NHibernate;
using NHibernate.Linq;
......
[HttpPost]
public ActionResult Edit(Post updatedPost)
{
ISession session = SQLiteUtil.SessionFactory.OpenSession();
IQueryable<Post> postRepository = session.Query<Post>();
Post oldPost = postRepository.Single(post => post.Id == updatedPost.Id);
if (ModelState.IsValid)
{
oldPost.Title = updatedPost.Title;
oldPost.Body = updatedPost.Body;
oldPost.CreatedBy = updatedPost.CreatedBy;
session.SaveOrUpdate(oldPost);
return RedirectToAction("Index");
}
return View(updatedPost);
}
5. Use SQLite
In order to use SQLite in our project, download the .NET driver from here. Reference the Dlls from your ASP.NET MVC 3 project.
Hope you enjoy the post. Happy coding!!