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.
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.
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 –
You can download the NHibernate DLL by using the following methodologies –
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.
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 –
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 –
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(); } } } public virtual string department_id" />
The last thing will be to close the session –
You can see your output being converted as shown below –
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.
The above is the detailed content of nhibernate in C#. For more information, please follow other related articles on the PHP Chinese website!