NHibernate - Basic ORM
NHibernate - Base ORM
In this chapter we are going to cover some base mappings and you know that since the last one chapter that we have the database table as well as the definition of the C # class. Now we need a mapping that explains how to translate from C # to database and back.
So let's go ahead and add a new XML file by right clicking on the project in Solution Explorer and selecting Add → New Item ...
in the name field. need to specify a default assembly which will be NHibernateDemoApp and also specify a default namespace. This just shortens a lot of the other type definitions we're going to do in this file.
Here is the implementation in the XML file -
The next thing we have need to define a class; this class will be our student class . Next we need to tell NHibernate the name of the id, which is ID and I also need to tell NHibernate how to generate ids, so our generator will be of native type.
The native type generator means that in a database like SQL Server it will use the identity column, the identity type.
The next thing we need to do is give the names of the properties. So add two more properties for the FirstName and the LastName.
Now we are reading these mapping files from the assembly, so the best way to do this is tointegrate these HBM files into your assembly. We can do this by simply setting a property.
Now right click on the project in Solution Explorer and select Properties, you will see the Build Action field where the content is selected by default.
Select the embedded resource from the drop-down list.
So this actually embeds this XML file inside the NHibernateDemoApp assembly.