Updated April 15, 2023
Introduction to nhibernate in C#
NHibernate is a very popular fully-featured tool that can be used as a solution for object-relational mapping in the .Net platform of Microsoft. It is one of the ports of Hibernate. We can map the domain model, which is object-oriented, to the relational database, which is traditional, by using this framework. In this nhibernate in C# article, we will look at what is NHibernate in C#, how to work and develop the project in it, how to get started and see its implementation, along with the help of an example.
What is nhibernate in c#?
The main feature of NHibernate is the mapping of the classes in C# or another platform such as .Net to the tables present in the relational databases such as MySQL. That also means that NHibernate is making the conversion of the datatype of CLR to SQL. NHibernate is also responsible for querying and retrieving the data, and there is no need to generate the SQL commands because NHibernate also handles that. Thus, the developer does not need to worry about object conversion. The application also remains portable for many SQL databases with almost none of the overhead to performance.
How to work nhibernate in c#?
You need to install the NHibernate and have an editor where you will be coding. Further, you should also have a database such as MySQL that you will be using in your application. We can make use of the editors such as Sublime text, visual studio, eclipse, or any other editor to create an NHibernate project. The most suggested editor is the visual studio. The screen of the visual studio looks as shown below –
Getting started nhibernate in c#
You can download the NHibernate DLL by using the following methodologies –
- Get the source code from Github – You can download the zip file of the source code of NHibernate from this link – https://github.com/nhibernate/nhibernate-core. The page will look as shown below, and you will have to click on the Code button to get the option of downloading the zip file –
- By using the package manager NuGet – If you have the NuGet package manager then you can go to the option of management software packages and then click on install NHibernate. The package manager window will look as shown below –
- You can download the zip file of the NHibernate from SourceForge. You can check the following website for downloading the NHibernate from SourceForge site – https://sourceforge.net/projects/nhibernate/. The home page of the site looks like as shown below –
Once you have got the zip file of NHibernate, then you can simply extract it in a particular folder in the specific directory. Now, you can easily add the references of the NHibernate DLLs in your project by simply referring to that directory.
Develop Project using nhibernate in c#
Creating a project of NHibernate in C# is quite easy, all you need to have is the visual studio code editor installed on your system. Note that the version of the visual studio should be 2008 or greater. The steps required to follow to create a project using NHibernate in C# are as shown below –
- Create a blank project in the visual studio. For this open the visual studio editor by searching it in the start search box of windows or clicking on the icon whose shortcut you have created anywhere. Click on the File option, choose the open folder, and make sure that you have created a new folder for your project. The visual window will look as shown below –
- Talking about the NHibernate project, there should be 4 main parts in it which are –
- To map the data of your application to POCOs, you will need a hibernate mapping file
- The configuration file of hibernate that is hibernating.cfg
- POCOs that are plain old CLR objects
- View page of MVC in apx or main class.
nhibernate in c# examples
Firstly, we will create a table in our database, for example say, Educba_writers. Our table in MySQL looks as shown below –
Now, we will create a new web project named EducbaWriterHiber and will set it in directory http://localhost/EducbaWriterHiber. We will then add the reference of NHibernate.dll. If you are using Visual Studio editor, it will automatically copy all the dependencies and libraries in the project. Then you will go for creating the XML file for mapping as shown below –
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" auto-import="true" assembly="EducbaWriterHiber" namespace="EducbaWriterHiber.Models">
<class name="Educba_writers" table="Educba_writers" dynamic-update="true" xmlns="urn:nhibernate-mapping-2.2">
<cache usage="read-write"/>
<id name="Id" column="writer_id" type="int">
<generator class="native" />
</id>
<property name="f_name" />
<property name="l_name" />
<property name="email_id" />
<property name="mobile_number" />
<property name="join_date" />
<property name="domain_id" />
<property name="pay_amount" />
<property name="guide_id" />
<property name="department_id" />
</class>
</hibernate-mapping>
Now, we will create a new configuration file, hibernate.cfg.xml, or register the entry in Web. config. Thereafter, you can create the POCO file named Educbawriter as shown below –
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace EMPNHibernate.Models
{
public class Employee
{
public virtual int writer_id { get; set; }
public virtual string f_name { get; set; }
public virtual string l_name { get; set; }
public virtual string email_id { get; set; }
public virtual string mobile_number { get; set; }
public virtual string join_date { get; set; }
public virtual string domain_id { get; set; }
public virtual string pay_amount { get; set; }
public virtual string guide_id" />
public virtual string department_id" />
}
}
Now, we will create the main class of ASX page that can be used as a singleton class having NHibernate session factory class in it –
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" auto-import="true" assembly="EducbaWriterHiber" namespace="EducbaWriterHiber.Models">
<class name="Educba_writers" table="Educba_writers" dynamic-update="true" xmlns="urn:nhibernate-mapping-2.2">
<cache usage="read-write"/>
<id name="Id" column="writer_id" type="int">
<generator class="native { get; set; }
</id>
public virtual string f_name { get; set; }
public virtual string l_name { get; set; }
public virtual string email_id { get; set; }
public virtual string mobile_number { get; set; }
public virtual string join_date { get; set; }
public virtual string domain_id { get; set; }
public virtual string pay_amount { get; set; }
public virtual string guide_id" />
public virtual string department_id" />
</class>
</hibernate-mapping>
Create an entry in Web.config
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace EMPNHibernate.Models
{
public class Employee
{
public virtual int writer_id { get; set; }
public virtual string f_name { get; set; }
public virtual string l_name { get; set; }
public virtual string email_id { get; set; }
public virtual string mobile_number { get; set; }
public virtual string join_date { get; set; }
public virtual string domain_id { get; set; }
public virtual string pay_amount { get; set; }
public virtual string guide_id" />
public virtual string department_id" />
}
}
using System.Web;
using NHibernate;
using NHibernate.Cfg;
namespace EducbaWriterHiber
{
public class EducbaWriterHiberSession
{
public static ISession OpenSession()
{
var sampleConfig = new Configuration();
sampleConfig.Configure();
ISessionFactory sampleSessFactory = sampleConfig.BuildSessionFactory();
return sampleSessFactory.OpenSession();
}
}
}
The last thing will be to close the session –
You can see your output being converted as shown below –
Conclusion
NHibernate in C# can be used as an open-source, free framework for ORM that is Object Relational Mapping. It is specially designed for the .Net framework and helps in creating persistent layers.
Recommended Articles
This is a guide to nhibernate in C#. Here we discuss what is NHibernate in C#, how to work and develop the project in it, how to get started, and also see its implementation. You may also have a look at the following articles to learn more –