Introduction
  • NHibernate is an object-relational mapping (ORM) solution for the Microsoft .NET platform.
  • NHibernate's primary feature is mapping from .NET classes to database tables.
  • NHibernate also provides data query and retrieval facilities.


Steps:

1. First you need to download Nhibernate. Download here...

2. Create an new ASP.Net Web Application. (In my case it’s called “NHibernateWeb”)

3. Add bellow mentioned NHibernate dll’s as references to the project.
  • NHibernate.dll
  • Iesi.Collections.dll

4. For this tutorial I’ll be using SQLServer2008 instance. 
  • I’ll be having a database called “NHibernateWeb”. 
  • Within that database I have table called “UserInfo” with following structure.
         Id – int
         Name – string
         Address - string
         Phone - string
         DateCreated - date  

 CREATE TABLE [dbo].[UserInfo]  
 (  
 [Id] [int] IDENTITY(1,1) NOT NULL,  
 [Name] [varchar](200) NOT NULL,  
 [Address] [varchar](200) NOT NULL,  
 [Phone] [varchar](200) NOT NULL,  
 [DateCreated] [datetime] NOT NULL,  
 CONSTRAINT [PK_User] PRIMARY KEY CLUSTERED   
 (  
 [Id] ASC  
 ) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]  
 ) ON [PRIMARY]  



5. Insert few records to "UserInfo" table
 INSERT INTO [UserInfo]([Name],[Address],[Phone],[DateCreated])  
 VALUES('Sameera Jayalath','Melbourne','0416213194','2014-08-30')  
 
6. Better if we can have the NHibernate contents in following folder structure.
  • Create a folder called “Models” within the project.
  • Under “Models” folder create another 3 folders called “Classes”, “Config”, “Mappings” 


7. Then inside “Classes” folder create a new class: UserInfo.cs
Important: We need create properties as virtual
 using System;
   
 namespace NHibernateWeb.Models.Classes  
 {  
   public class UserInfo  
   {  
     public virtual int Id { get; set; }  
     public virtual string Name { get; set; }  
     public virtual string Address { get; set; }  
     public virtual string Phone { get; set; }  
     public virtual DateTime DateCreated { get; set; }  
   }  
 }  

8. Inside “Config” folder create a new xml called: hibernate.cfg.xml
Important: We need to set following properties for the file
Build Action: Embeded Resources
Copy to Output Directory: Copy Always
 <?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="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>  
   <property name="connection.connection_string">Data Source=localhost;Initial Catalog=NHibernateWeb;User ID=xyz;Password=pqr;</property>  
   <property name="dialect">NHibernate.Dialect.MsSql2008Dialect</property>  
   <property name="current_session_context_class">web</property>  
  </session-factory>  
 </hibernate-configuration>  




9. Inside “Mappings” folder create another xml called: UserInfo.hbm.xml
Important: We need to name the file to align with the class name
Important: We need to set following properties for the file
Build Action: Embeded Resources
Copy to Output Directory: Copy Always

 <?xml version="1.0" encoding="utf-8" ?>  
 <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"  
           assembly="NHibernateWeb"  
           namespace="NHibernateWeb.Models.Classes">  
   
  <class name="UserInfo">  
   
   <id name="Id">  
    <generator class="identity" />  
   </id>  
   <property name="Name" />  
   <property name="Address" />  
   <property name="Phone" />  
   <property name="DateCreated" />  
   
  </class>  
   
 </hibernate-mapping>  
 

 10. Add a web form and add following code to the Page_Load event.
 using NHibernate;  
 using NHibernate.Cfg;  
 using NHibernateWeb.Models.Classes;  
 using System;  
 using System.Web;  
   
 namespace NHibernateWeb  
 {  
   public partial class Home : System.Web.UI.Page  
   {  
     protected void Page_Load(object sender, EventArgs e)  
     {  
       try  
       {  
         var configuration = new Configuration();  
         var hibernateConfigurationPath = HttpContext.Current.Server.MapPath(@"~\Models\Config\hibernate.cfg.xml");  
         configuration.Configure(hibernateConfigurationPath);  
         var refCallTypeConfigurationFile = HttpContext.Current.Server.MapPath(@"~\Models\Mappings\UserInfo.hbm.xml");  
         configuration.AddFile(refCallTypeConfigurationFile);  
         ISessionFactory sessionFactory = configuration.BuildSessionFactory();  
         ISession session = sessionFactory.OpenSession();  
   
         var UserList = session.CreateCriteria<UserInfo>().List<UserInfo>();  
   
         foreach (var UserItem in UserList)  
         {  
          
 string userInfo = string.Format("Id: {0} <br/> Name: {1} 
<br/> Address: {2} <br/> Phone: {3} 
<br/> Date Created: {4} 
<br/><br/>", UserItem.Id, UserItem.Name, 
UserItem.Address, UserItem.Phone, 
UserItem.DateCreated.ToShortDateString());  
           Response.Write(userInfo);  
         }  
       }  
       catch (Exception ex)  
       {  
         Response.Write(ex.ToString());  
       }  
     }  
   }  
 }  


  
That’s it. This concludes the Part I - Getting started with NHibernate

Part II - NHibernate SessionFactory to be followed...

Happy coding !!!
0

Add a comment

In this tutorial I’m going to show how to:
• Create an ASP.NET Core MVC Application and Publish to Azure
• Create/Purchase a Custom Domain from a Custom Domain Provider
• Configure Custom Domain on Azure
Alt Text
You can find the PDF version of this tutorial here.
You can find all my Azure/.Net tutorials here and here.
Enjoy !!
0

Add a comment

About Me
About Me
Blog Archive
Loading
Dynamic Views theme. Powered by Blogger. Report Abuse.