In this section, we will look at how to start an easy instance using NHibernate. We will be constructing a simple console application. To create a console application, we will use Visual Studio 2015, which covers all of the features you need to create, test your application using the NHibernate package.
Resulting are the steps to create a project using project templates available in the Visual Studio.
Step 1 − Open the Visual studio and click File → New → Project menu option.
Step 2 − A new Project dialog opens.
Step 3 − From the left pane, select Templates → Visual C# → Windows.
Step 4 − In the middle pane, select Console Application.
Step 5 − Enter the project name, ‘NHibernateDemoApp’, in the Name field and click Ok to continue.
Step 6 − Once the project is created by Visual Studio, you will see a number of files displayed in the Solution Explorer window.
As you see that we have formed a simple console application project, currently we need to comprise the NHibernate package to our console project.
Go to the Tools menu and select NuGet Package Manager → Package Manager Console, it will open the Package Manager Console window.
Insist on the command revealed in the above Package Manager Consolewindow and presses enter, it will download all the NHibernate needs and create references to all of the required assemblies. As soon as the installation is over, you will see the note as shown in the following image.
Currently that we have NHibernate added, we can now start implementation. So, we are accepted to start out by mapping a very simple table called Student, which merely has an integer primary key, called ID and a FirstName and LastName column.
We need a class to signify this student, so let’s make a new class called Student by right clicking on the project in the solution explorer and then select Add → Class which will open the Add New Item dialog box.
Enter Student.cs in the name field, click the Add button. In this Student class, we need to have our integer primary key called ID, and we need to create this string, FirstName and LastName fields as shown in the following complete implementation of Student class.
As soon as dealing with models in NHibernate application, it is coolest to make all of your fields essential. So this is our simple NHibernate model that we will use and will map this to the back end database.
Now let’s go to the Main method in the Program class and create a new NHibernate configuration object.
The first thing that we need to provide is the connection string. This is a database specific connection string and the easiest way to find the connection string is that right click on the database in SQL Server Object Explorer and select Properties.
It will open the Properties Window, now scroll down and you will see the Connection String field in the Properties window.
Copy the Connection string and specify in your code. Following is the implementation of Main method in which we need configuration for NHibernate.
After the association string, we want to supply a driver, which is the SQLClientDriver and then we also need to provide it a dialect, which version of SQL Server, and we are going to use MS SQL 2008.
NHibernate now tells how to connect to the database. The extra thing we need to do is to offer it a list of models that we will map.
We can do this by adding an association, so by specifying the Assembly.GetExecutingAssembly and this is where program will find mapping files. Mapping files tell NHibernate how to go from C# classes into database tables.
SessionFactory amasses all the metadata essential for initializing NHibernate. SessionFactory can be used to build sessions, which are roughly analogous to database connections. So the suitable way is to use it in the using block. I can say var session equals sessionFactory.OpenSession and I'm going to want to do this inside of its transaction.
Once the session is opened, we can tell the session to begin a new transaction and we can then perform some logic in here. So do some database logic and lastly commit that transaction.
NHibernate Related Interview Questions
|SQL Server 2008 Interview Questions||ASP.NET Interview Questions|
|LINQ Interview Questions||IBM Tivoli Interview Questions|
|Windows Presentation Foundation(WPF) Interview Questions||Windows Communication Foundation (WCF) Interview Questions|
|Ibm Tivoli Netcool Omnibus Interview Questions||Dynamic Link Library (DLL) Interview Questions|
|Advanced C# Interview Questions||Asp Dot Net Mvc 4 Interview Questions|
All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd
Wisdomjobs.com is one of the best job search sites in India.