Introduction
Running Nhibernate on Sharepoint is not an easy task. It might take 1 or 2 days to do it. In this tutorial, I will try to make this task easy by providing a step by step guide starting from how to download NHibernate to how to configure it on SharePoint.
You can download the latest Nhibernate version from here.
In my tutorial, I am assuming that you know what’s NHibernate and how to use it, if it’s the first time you hear about NHibernate, please read the following tutorial first:
HBM & CS Classes Generators
There are many ways to generate HBM and CS classes:
- MYGeneration Templates: I do not recommend these templates because most of them are not updated to NHibernate newer versions and it’s not easy to change in its templates
Download link: http://www.mygenerationsoftware.com - Smart Code Studio: It’s a better way to generate the hbm files and CS classes, I’ve modified in its template to be more standard and simple, you can download my template from this link.
(if you are not using Smart Code Studio, you can watch a video on how to use it from this link http://www.kontac.net/site/SmartCode/tabid/55/Default.aspx)
Download link: http://www.kontac.net/site/SmartCode/Download/tabid/64/Default.aspx
Using NHibernate in Your Sharepoint Project
Create a new class library project in your solution called ABC.Domain
and generate the hbm files and cs classes, then place into it.
Make sure all hbm classes are compiled as Embedded resources:

Create another class library project called ABC.BL
to use the Domain project and to be used by the Sharepoint project, don’t forget to add both classes to GAC using gacutil command.
Extract the Nhibernate folder and make sure to add reference to the following DLLs + Adding reference to your Domain project:
- Remotion.Data.Linq.dll
- NHibernate.dll
- Nhibernate.ByteCode.Linfu.dll
- NHibernate.ByteCode.Castle.dll
- log4net.dll
- LinFu.DynamicProxy.dll
- Iesi.Collections.dll
- Castle.DynamicProxy2.dll
- Castle.Core.dll
Antlr3.Runtime.dll

After adding the references to your project, make sure to install them to GAC by executing the following commands on Visual Studio Command Prompt:
gacutil -i "\Remotion.Data.Linq.dll"
gacutil -i "\NHibernate.dll"
gacutil -i "\Nhibernate.ByteCode.Linfu.dll"
gacutil -i "\NHibernate.ByteCode.Castle.dll"
gacutil -i "\log4net.dll"
gacutil -i "\LinFu.DynamicProxy.dll"
gacutil -i "\Iesi.Collections.dll"
gacutil -i "\Castle.DynamicProxy2.dll"
gacutil -i "\Castle.Core.dll"
gacutil -i "\Antlr3.Runtime.dll"
gacutil -i "\ABC.Domain\Bin\Debug\ABC.Domain.dll"
Configure your Web.config & hibernate.cfg.xml.
Prepare your hibernate.cfg.xml and place it into your website bin folder (e.g. C:\inetpub\wwwroot\wss\VirtualDirectories\80\bin)

Now all references and configurations are ready, but if you tried to deploy your solution, you will get a run time error “Could not load file or assembly 'xyz' or one of its dependencies”. Now you will just need to add qualifyAssembly
tags to your web.config (e.g., C:\inetpub\wwwroot\wss\VirtualDirectories\80\ ) to allow Nhibernate to load assemblies from GAC and not to expect it in the bin folder.

To use Nhibernate inside your Sharepoint project, you can refer to this tutorial: