I had a tough time configuring and setting up NHibernate with the configuration that I wanted. This is what I wanted to do:
1. Database first
2. Fluent Nhibernate
I know this kind of looks like the basic stuff that every one wants to do. But I wanted code for fluent Nhibernate to be generated by some tool. Finally I got hold of NHibernate Mapping Generator. A very simple to use mapping generator and free :). You can find it here: Nhibernate Mapping Generator.
The only problem that I faced with this tool is that if a table has a space in its name the code generator will add and space in the class name which breaks everything. So before you use this tool to generate the mapping classes make sure you remove any spaces from the table name. I never add spaces when I am creating a table but Northwind database has tables with spaces in table name.
These are the steps that I followed for NHibernate Mapping Generator:
1. Database first
2. Fluent Nhibernate
I know this kind of looks like the basic stuff that every one wants to do. But I wanted code for fluent Nhibernate to be generated by some tool. Finally I got hold of NHibernate Mapping Generator. A very simple to use mapping generator and free :). You can find it here: Nhibernate Mapping Generator.
The only problem that I faced with this tool is that if a table has a space in its name the code generator will add and space in the class name which breaks everything. So before you use this tool to generate the mapping classes make sure you remove any spaces from the table name. I never add spaces when I am creating a table but Northwind database has tables with spaces in table name.
These are the steps that I followed for NHibernate Mapping Generator:
- Downloaded NHibernate Mapping Generator
- After running NHibernateMappingGenerator.exe from the dowloaded zip file I entered the connection string pointing to my local Northwind database. (Make sure to remove spaces from table name)
- select the path where I want the generated files to be placed.
- In preferences make sure the Fluent Nhibernate is selected.
- Hit Generate All
Using the generated files in Visual studio:
I created a solution with a class library "DataLayer" and added two folders "Entities" and "Mapping".
I added one more file NhibernateSession for maintaining a single session just to keep things simple. Also to test the generated classes I added a test project which is a console application.
I added three references to the "DataLayer" & "Test" project: FluentNhibernate, NHibernate, NHibernate.ByteCode.Castle.
These are the contents of NhibernateSession class:
The Test project contains a single file Program.cs and its content are as follows:
Thats it. Hit run and enjoy. NHibernate setup in may be 10 minutes :).
If you need source code for this sample project write in comments. I figured its just too simple for the need to include any source code.
I added three references to the "DataLayer" & "Test" project: FluentNhibernate, NHibernate, NHibernate.ByteCode.Castle.
These are the contents of NhibernateSession class:
public class NhibernateSession { private static ISessionFactory _sessionFactory; private static ISessionFactory SessionFactory { get { if (_sessionFactory == null) InitializeSessionFactory(); return _sessionFactory; } } private static void InitializeSessionFactory() { _sessionFactory = Fluently.Configure() .Database(MsSqlConfiguration.MsSql2008.ConnectionString(@"Server=.;Database=Northwind;Trusted_Connection=True;").ShowSql()) .Mappings(m => m.FluentMappings.AddFromAssemblyOf<Order>()) .BuildSessionFactory(); } public static ISession OpenSession() { return SessionFactory.OpenSession(); } }
The Test project contains a single file Program.cs and its content are as follows:
class Program { static void Main(string[] args) { TestRead(); } private static void TestRead() { using (var session = NhibernateSession.OpenSession()) { var makeQuery = (from product in session.Query<Product>() select product).First(); Console.WriteLine("Product Name: " + makeQuery.ProductName); Console.ReadLine(); } } }
Thats it. Hit run and enjoy. NHibernate setup in may be 10 minutes :).
If you need source code for this sample project write in comments. I figured its just too simple for the need to include any source code.