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 –
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 –
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.
The above is the detailed content of nhibernate in C#. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics





Guide to Active Directory with C#. Here we discuss the introduction and how Active Directory works in C# along with the syntax and example.

Guide to C# Serialization. Here we discuss the introduction, steps of C# serialization object, working, and example respectively.

Guide to Random Number Generator in C#. Here we discuss how Random Number Generator work, concept of pseudo-random and secure numbers.

Guide to C# Data Grid View. Here we discuss the examples of how a data grid view can be loaded and exported from the SQL database or an excel file.

Guide to Patterns in C#. Here we discuss the introduction and top 3 types of Patterns in C# along with its examples and code implementation.

Guide to Factorial in C#. Here we discuss the introduction to factorial in c# along with different examples and code implementation.

Guide to Prime Numbers in C#. Here we discuss the introduction and examples of prime numbers in c# along with code implementation.

The difference between multithreading and asynchronous is that multithreading executes multiple threads at the same time, while asynchronously performs operations without blocking the current thread. Multithreading is used for compute-intensive tasks, while asynchronously is used for user interaction. The advantage of multi-threading is to improve computing performance, while the advantage of asynchronous is to not block UI threads. Choosing multithreading or asynchronous depends on the nature of the task: Computation-intensive tasks use multithreading, tasks that interact with external resources and need to keep UI responsiveness use asynchronous.
