.NET NHibernate tutorial #1 – Project setup
This article is the first of series of articles about using .NET and NHibernate.
In the series we work with tools that can be used in commercial projects freely:
1. Visual Studio 2012 Express for Desktop http://bit.ly/QLJsJT (note that this tool is dedicated to creating desktop applications. If we want to build web-based applications, we need to install Visual Studio 2012 Express for Web)
2. SQL Server 2012 Express http://bit.ly/KJIAMR
3. NHibernate 3.3.3. package http://nhforge.org/
To prepare the .NET NHibernate environment in Visual Studio 2012 Express we need to create a new project by choosing a project type, for example
Console Application, giving a name to our project and indicating the location of our files (Capture 01).
Now we can see how the created project looks (Capture 02).
Then we need a NHibernate package. We can download it from the website: http://nhforge.org/ as a zip package. When we unzip NHibernate, we will see folders and files like below: (Capture 03)
The best practice is to copy all files from the
Required_Bins folder to our project. We can make a new folder called
NHibernate and copy files there, like bellow: (Capture 04)
We can copy all files into our folder, but the critical files are two dlls only:
NHibernate.dll. For easier configuration we also need two more files:
nhibernate-mapping.xsd (see Capture 05).
Next step is adding NHibernate reference files to our project. To do that, right-click
Reference and choose
Add Reference (Capture 06).
As the result, we will see the references group. Then, go to the
Browse tab and click
Browse... (Capture 07).
Next, let us add references from the folder where we copied files. (Like in Capture 08, we can find it in our project folder). When we select both references (
NHibernate.dll) we click
Now, we see that references are added and marked (see Capture 09).
When we click button
OK we will see that references
NHibernate.dll are added to our project (Capture 10).
In case of configuring NHibernate with xml files, we can add schemas to make it easier. For example, in our project we have a file named
App.config (that is the xml file). When we open this file we will see a new menu item called
XML (Capture 11).
Next, we click
Schemas.... Now, we see all xml schemas that are added or can be added to the project (Capture 12).
To add NHibernate schemas to our project we click
Add... on the right site of the dialog. It results in opening a window where we can find two xsd files called
nhibernate-mapping.xsd. We need to select them both and click button
Open (Capture 13).
We will see what we have added in the
XML Schemas window. As they are marked, we need to click
OK only (Capture 14).
Now we can start configuring NHibernate in our project.
Download this sample code here.
This code is available on our GitHub repository as well.