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"></cache> <id name="Id" column="writer_id" type="int"> <generator class="native"></generator> </id> <property name="f_name"></property> <property name="l_name"></property> <property name="email_id"></property> <property name="mobile_number"></property> <property name="join_date"></property> <property name="domain_id"></property> <property name="pay_amount"></property> <property name="guide_id"></property> <property name="department_id"></property> </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!

C# is widely used in enterprise-level applications, game development, mobile applications and web development. 1) In enterprise-level applications, C# is often used for ASP.NETCore to develop WebAPI. 2) In game development, C# is combined with the Unity engine to realize role control and other functions. 3) C# supports polymorphism and asynchronous programming to improve code flexibility and application performance.

C# and .NET are suitable for web, desktop and mobile development. 1) In web development, ASP.NETCore supports cross-platform development. 2) Desktop development uses WPF and WinForms, which are suitable for different needs. 3) Mobile development realizes cross-platform applications through Xamarin.

The C#.NET ecosystem provides rich frameworks and libraries to help developers build applications efficiently. 1.ASP.NETCore is used to build high-performance web applications, 2.EntityFrameworkCore is used for database operations. By understanding the use and best practices of these tools, developers can improve the quality and performance of their applications.

How to deploy a C# .NET app to Azure or AWS? The answer is to use AzureAppService and AWSElasticBeanstalk. 1. On Azure, automate deployment using AzureAppService and AzurePipelines. 2. On AWS, use Amazon ElasticBeanstalk and AWSLambda to implement deployment and serverless compute.

The combination of C# and .NET provides developers with a powerful programming environment. 1) C# supports polymorphism and asynchronous programming, 2) .NET provides cross-platform capabilities and concurrent processing mechanisms, which makes them widely used in desktop, web and mobile application development.

.NETFramework is a software framework, and C# is a programming language. 1..NETFramework provides libraries and services, supporting desktop, web and mobile application development. 2.C# is designed for .NETFramework and supports modern programming functions. 3..NETFramework manages code execution through CLR, and the C# code is compiled into IL and runs by CLR. 4. Use .NETFramework to quickly develop applications, and C# provides advanced functions such as LINQ. 5. Common errors include type conversion and asynchronous programming deadlocks. VisualStudio tools are required for debugging.

C# is a modern, object-oriented programming language developed by Microsoft, and .NET is a development framework provided by Microsoft. C# combines the performance of C and the simplicity of Java, and is suitable for building various applications. The .NET framework supports multiple languages, provides garbage collection mechanisms, and simplifies memory management.

C# and .NET runtime work closely together to empower developers to efficient, powerful and cross-platform development capabilities. 1) C# is a type-safe and object-oriented programming language designed to integrate seamlessly with the .NET framework. 2) The .NET runtime manages the execution of C# code, provides garbage collection, type safety and other services, and ensures efficient and cross-platform operation.


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

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool
