Before we actually start, let’s give a brief outline of the tools we use for the O/R mapping:
- Windows 7
- Visual Studio Express 2010 Beta 2 (I took the offline installer there)
- SQL Server 2008 Express (included with the offline installer iso)
- NHibernate 2.1.2.GA
Important: When installing VS2010 Express Beta, you will be asked whether or not to install SQL Server Express 2008 SP1. Do that.
Extract the NHibernate binaries to a local directory.
1.1 Preparing the DB
Before we actually start mapping database records to objects, we need to have what? Correct, a database! So we simply create our own database. The purpose of this tutorial is not to show how to create a DB, so I put it simply as a series of screenshots with captions:
Figure 1: The Database Explorer of Visual C# Express is a tool for exploring and modifying databases. If not shown upon VS C# startup, simply hit [CTRL + W, L] to display it. Here, right-click on “Data connections”, and subsequently on “Add connection”.
Figure 2: We will create a DB and store it in a file. Select as shown on the screenshot and proceed with “Continue”.
Figure 3: Only field to concern is Database file name. Enter a new file name or choose an existing one. If the user you installed SQL Express is the same as you are developing with now, you are fine leaving the options as on the screenshot.
Figure 4: By right-clicking on Tables, you can select “Add Table” to create a new table. Do that, and create a table in designer mode just as shown on the screenshot. Insert some data, too:
Fast, isn’t it? At this point, you should not have wasted more than 15-20 Minutes, even if you are new to this way of creating a DB. Now we are going to set up a project that can actually transfer our Book entity from the DB to a .NET class.
1.2 Setting up NHinbernate
Our first step is to create a new application. For now, a simple Console application will do. Let’s call it DevJour1.
Next, we need to add the necessary DLLs in order to get nhibernate running
Create a subfolder lib in your project’s directory (e.g. C:\DevJour1\lib)
Copy all DLLs from the Required_Bins directory (of the extracted nhibernate zip file) into the \lib subfolder
Copy all DLLs from Required_For_LazyLoading\Castle directory (of the extracted nhibernate zip file into \lib
In VS, right-click on the project, ->References and go to Add reference. Add the DLLs from our newly created subfolder as shown on the screenshot:
Let’s This was it already! We’re set up and ready for the mapping! Let’s move on to Part 2!