I have worked with both Entity Framework (at work) and NHibernate (at private projects) for a while now, and I tought I would write a post about the differance between the two. They are both (kind of) Object Relational Mappers (ORM´s) and they were developed to solve the same problem, that is data access in a .NET application. Entity Framework is a product from Microsoft while NHibernate is a Open Source project.
As everyone who have built a data dependent application knows, data access code can be pretty repetative. You do the same thing over and over again in application after application. For this reason we have ORM´s, they abstract the unnecessary repeating of ADO.NET code away from the application and we can instead query our domain model in the way we prefer. In my oppinion, a application developer should not have to think about the database model, we should leave that to the dba. A ORM eliminates this need as we just have to create our domain model and query it as a abstraction of the acctuall data storage.
NHibernate
The differance between the two frameworks, nhibernate and entity framework, is pretty big. With NHibernate you map your entities to your database model with xml files. You have one xml file for each class you want to map where you specifie how the object will be persisted and what relations it has to other objects and how they are related. You then have a xml file for configuration where you tell NHibernate where to find the mappings you have specified, what kind of database to use, what connectionstring to use and a few more options.
Then when you want to query your objects you use a ISession that basicly is a connection to your database. There is four different ways to query with NHibernate out of the box. You can use hql (wich is a sql-like language that queryes the object instead of a table), criteria (wich is a fluent interface to chain method calls and specifie different criterias for the query), stored procedures and normal sql queries. They do all have different pros and cons. The most ORM like way to query is using the Criteria api. This is a high level abstraction and the most easy to understand for most developers. While at the low level you have stored procedures and normal sql queries. At a lower level you do offcourse have more control of what data to get and you can optimice your queries more then with a high level abstraction. In most senarious a high level approach is preferable as it´s easier to maintain and do not have as many dependencies as the low level queries have.
A good thing with NHibernate is that it is a very mature framework with a Java version (Hibernate) that has been out for a long time. There is also some very good extensions for NHibernate. You have FluentNHibernate that makes mapping easier with a fluent interface where you map with c# (or the language of youe choise) code. You have a implementation of Linq for NHibernate that enables querying with Linq if you prefer that over the other options. You have NHibernate search that uses Lucene.NET, a full text search engine, to enable free text search with good preformance. This among other libraries make NHibernate a very flexible framework with alot of options to suit many developers.
Entity Framework
With Entity Framework you have a more visuall mapping technique where you have a ".edmx" file that you use to visually map your database. You can here specifie what database to use and you will have the option to import the tables that you want. Entity Framework will then generate your domain model from this and create classes with the appropriat relationships. This is a very easy and fast mapping that will be done very quickly. The NHibernate mapping will take more time to complete and there is more that you have to do on your own. Even if you have a library like FluentNHibernate that helps reduce the coding alot with some conventions. The problem with Entity Frameworks easy mapping is that it does only support MS SQL Server while NHibernate supports all the popular databases. On the other side, the acctuall access to the database and query generation can be a bit more optimiced with Entity Framework as you only have one database to think about, and they are both Microsoft products.
To query your objects with Entity Framewok you use Linq or stored procedures. You do this by creating a instance of the data context that will be generated with your domain model. The context will then contain you entities ready to be queried. If you have stored procedures you can map them in the same way you map your tabels. The data context will then contain a method with the same name as the stored procedure with the requiered parameters and Entity Framework will define a object that will be returned from the method that calls the stored procedure. This is a very easy-to-use approach that I like, even if I dont use stored procedures very often.
One thing to note here is the options for lazy loading. Lazy loading is where you load a refering object/collection at the time when you use it, and not before or when you dont need to use it. With NHibernate this is very easy. You only have to declare your properties of your entities as virtual and NHibernate will take care of the rest. You can use your entities in any way you want and not notice what is acctually happening in the background. With the current version of Entity Framework however, there is no implementation for lazy loading. This will however be present in the next version that is to be shipped with visual studio 2010 later this year. With the current version of Entity Framework you will instead explicitly define what releations to load each time you query a object. You can do this eather by using the ".Include" method and specifie the name of the object or collection that you want to include in your query. Or you can use a nested query as described in this post.
Conclution
I feel that NHiberate is a more mature framework as of the current versions. And it does, in my opinion, follow the general principals of object oriented programing alot better. Entity Framework isn´t realy a ORM in the same sence as NHibernate. While NHibernate includes all the features that you can expect from a full grown ORM framework, Entity Framework is more like just a abstraction of the database itself in the current version. I feel that, right now, NHibernate is a more complete framework that includes the features that I look for in a ORM. In my oppinion, Entity Framework isn´t quite there yet. Lets see what will happen in the next version that will ship later this year.