How to set up NHibernate with PostgreSQL in ASP.NET

Author: Veeresh Rudrappa. Date: March 3, 2013

If you are looking for an Object Relational Mapping Solution for your .NET project then NHiberante is one framework I can vouch for. There are other ORM solutions for .NET like SubSonic , LLBLGen. NHibernate is open source and can be downloaded from here. If you are new to NHibernate, setting it up with your ASP.NET Application could be tricky. In this tutorial I will be showing you how to integrate NHibernate, PostgreSQL with ASP.NET MVC 3 application. In the next tutorial I will explain how to use NHibernate with PostgreSql in ASP.NET MVC to save objects

Step 1: Start a new ASP.NET MVC 3 Internet Application project in your Visual Studio.

Step 2: Download NHibernate from here. Add the following DLL's to your project references.


Step 3: Download Npgsql from here .
Npgsql is a .Net Data Provider that allows .Net client application to send and receive data with a PostgreSQL server. Add Npgsql.dll to your project references.

Step 4: Add the following lines of code to your Web.Config

Make sure configsections is the first child of configuration tag.
Add your Database Connection String.
Add finally add NHibernate Configuration.

Step 5: Create a Singleton class to hold the NHibernate Session Factory. Building a Session Factory is a very compute intensive task. So it is advisable to build it once and store it in a singleton class and share it across the instances. In this example I have created a singleton class called ApplicationCore.

                                                public sealed class ApplicationCore
                                                    private static readonly ApplicationCore mInstance = new ApplicationCore();
                                                    private static ISessionFactory mIsessionFactory;

                                                    public static ApplicationCore Instance
                                                        get { return mInstance; }

                                                    public ISessionFactory SessionFactory
                                                        get { return mIsessionFactory; }
                                                        set { mIsessionFactory = value; }

Step 6: Create an Instance of ApplicationCore Class in the Application_start method of the Glabal.asax.cs file and build the Session Factory.

Add the following lines of code to your Application_Start method in Global.asax.cs.
                                            Configuration configuration = new Configuration();
                                            ApplicationCore.Instance.SessionFactory = configuration.BuildSessionFactory();
configuration.Configure() will configure the NHibernate using the section we added in the Web.Config file.

Step 7: An HTTP module is an assembly that is called on every request that is made to your application. HTTP modules are called as part of the request pipeline and have access to life-cycle events throughout the request. So we will have to open a NHibernate session for the current HTTP request and bind it with the current session context. When HTTP request has been processed and response sent to the client we can unbind the session from the current context. We can create custom HTTP modules by implementing the IHttpModule interface.

                                                    public class NHIbernateHttpModule :IHttpModule

                                                        public void Dispose()

                                                        public void Init(HttpApplication context)
                                                            context.BeginRequest += new EventHandler(BeginEventhandler);
                                                            context.EndRequest += new EventHandler(EndEventhandler);

                                                        private void BeginEventhandler(object o, EventArgs e)
                                                            var session = ApplicationCore.Instance.SessionFactory.OpenSession();

                                                        private void EndEventhandler(object o, EventArgs e)
Add the Following lines to Web.config to register the above HttpModule.

Hope this Helps :). In the next tutorial I'll show how to map Application level class objects with Database using NHibernate.

comments powered by Disqus