Tuesday, March 12, 2013

Entity Framework 5 and 6 vs NHibernate 3 – The State of Affairs

It has been almost two years since I've last compared NHibernate and Entity Framework, so with the recent alpha version of EF 6, it's about time to look at the current state of affair. I've been using NHibernate for more than 6 years so obviously I'm a bit biased. But I can't ignore that EF's feature list is growing and some of the things I like about the NHibernate eco-system such as code-based mappings and automatic migrations have found a place in EF. Moreover, EF is now open-source, so they're accepting pull requests as well.

Rather than doing a typical feature-by-feature comparison, I'll be looking at those aspects of an object-relational mapper that I think are important when building large-scale enterprise applications. So let's see how those frameworks match up. Just for your information, I've been looking at Entity Framework 6 Alpha 3 and NHibernate 3.3.1GA.

Support for rich domain models
When you're practicing Domain Driven Design it is crucial to be able to model your domain using the right object-oriented principles. For example, you should be able to encapsulate data and only expose properties if that is needed by the functional requirements. If you model an association using a UML qualifier, you should be able to implement that using a IDictionary<T,T>. Similarly, collection properties should be based on IEnumerable<T> or any of the newer read-only collections introduced in .NET 4.5 so that your collections are protected by external changes.

NHibernate supports all these requirements and adds quite a lot of flexibility like ordered and unordered sets. Unfortunately, neither EF5 or 6 supports mapping private fields (yet) nor can you directly use a dictionary class. In fact, EF only supports ICollections of entities, so collections of value objects are out of the question. One notable type that still isn't fully supported is the enum. It was introduced in EF5, but only if you target .NET 4.5. EF6 will fortunately fixes this so that it is also available in .NET 4.0 applications.

A good ORM should also allow your domain model to be as persistence ignorant as possible. In other words, you shouldn't need to decorate your classes with attributes or subclass some framework-provided base-class (something you might remember from Linq2Sql). Both frameworks impose some limitations such as protected default constructors or virtual members, but that's not going to be too much of an issue.

Vendor support
Although Microsoft makes us believe that corporate clients only use SQL Server or SQL Azure, we all know that the opposite is much more true. The big drawback of EF compared to NH is that the latter has all the providers built-in. So whenever a new version of the framework is released you don't have to worry about vendor support.

Both EF5 and NH 3.3 support various flavors of SQL Server/Azure, SQLite, PostgreSQL, Oracle, Sybase, Firebird and DB2. Most of these providers originate from EF 4, so they don’t support code-first (migrations) or the new DBContext façade. EF6 is still an alpha release and its provider model seems to contain some breaking changes so don't expect any support for anything other than Microsoft's own databases anytime soon.

Support switching databases for automated testing purposes
Our architecture uses a repository pattern implementation that allows swapping the actual data mapper on-the-fly. Since we're heavily practicing Test Driven Development, we use this opportunity to approach our testing in different ways.

  1. We use an in-memory Dictionary for unit tests where the subject-under-test simply needs some data to be setup in a specific way (using Test Data Builders).
  2. We use an in-memory SQLite database when we want to verify that NHibernate can process the LINQ query correctly and performs sufficiently using NHProf.
  3. We use an actual SQL Server for unit tests that verify that our mapping against the database schema is correct.
  4. We have some integration code that interacts with a third-party Oracle system that is tested on SQL Server on a local development box, but uses Oracle on our automated SpecFlow build.

So you can imagine switching between database providers without changing the mapping code is quite essential for us.

During development, we decided that we did not care about the actual class that represented the integration tables, so we tried to use the Entity Framework model-first approach. Unfortunately, when you do that, you're basically locking yourself to a particular database. After switching back to our normal NHibernate approach, changing the connection string during deployment was enough to switch between SQL Server and Oracle. Fortunately this has also been possible since EF 4.1 and Jason Short wrote a good blog post about that.

Automatic schema migration
When you're practicing an agile methodology such as Scrum, you'll probably try to deliver a potentially shippable release at the end of every sprint. Part of being agile is that functionality can be added at any time where some of that might be affecting the database schema. The most traditional way of dealing with that is to generate or hand-write SQL scripts that are applied during deployment. The problem with SQL scripts is that they are tedious to write, might contain bugs, and are often closely coupled to the database vendor. Wouldn't it be great if the ORM framework would support some way of figuring out what version of the schema is being used and automatically upgrade the database scheme as part of your normal development cycle? Or what about the ability to revert the schema to an older version?

The good news that this exists for both frameworks, but with a caveat. For instance, NHibernate doesn't support this out-of-the-box (although you can generate the initial schema). But with the help of another open-source project, Fluent Migrations, you can get very far. We currently use it in an enterprise system and it works like a charm. The caveat is that the support for the various databases is not always at the same level. For instance, SQLite doesn't allow renaming a column and Fluent Migrations doesn't support it (although theoretically it could create a new column, copy the old data over, and drop the old column). As an example of a fluent migration supporting both an update as well as a rollback, check out this snippet.

public class TestCreateAndDropTableMigration: Migration
public override void Up()





Insert.IntoTable("TestTable").Row(new { Name = "Test" });

public override void Down()

Entity Framework has something similar built-in since version 5. It's called Code-First Migrations and looks surprisingly similar to Fluent Migrations. Just like the NHibernate solution has some limitations, EF's has as well and that is support from vendors. At the time of this writing not a single vendor supports Code-First Migrations. On the other hand, if you're only using SQL Server, SQL Express, SQL Compact or SQL Azure, there's nothing from stopping you to use it.

Code-based mapping
If you remember the old days of NHibernate, you might recall those ugly XML files that were needed to configure the mapping of your .NET classes to the underlying database. Fluent NHibernate has been offering a very nice fluent API for replacing those mappings with code. Not only does this prevent errors in the XML, it is also a very refactor-friendly approach. We've been using it for years and the extensive (and customizable) convention-based mapping engine even allows auto-mapping entities to tables without the need of explicit mapping code.

Strangely enough, NHibernate 3.2 has introduced a brand new fluent API that directly competes with Fluent NHibernate. Because of lack of documentation, I've never bothered to look at it, especially since Fluent NHibernate has been doing its job remarkedly. But during my research for this post I noticed that Adam Bar has written a very extensive series on the new API, and he actually managed to raise a renewed interest in the new API.

Until Entity Framework 4.1 the only way to set-up the mapping was through its data model designer (not to be confused with an OO designer). But apparently the team behind it learned from Fluent Nhibernate and decided to introduce their own code-first approach, surprisingly named Code-First. In terms of convention-based mapping, it was quite limited, especially compared to Fluent NHibernate. EF 6 is going to introduce a lot of hooks for changing the conventions, both on property level as well as on class level.

Supporting custom types and collections
One of the guidelines in my own coding guidelines is to consider wrapping primitive types with more domain-specific types. Conincedentily it is also one of the rules of Object Calisthenetics. In Domain Driven Design these types are called Value Objects and their purpose is to encapsulate all the data and behavior associated with a recurring domain concept. For instance, rather than having two separate DateTime properties to represent a period and separate methods for determining whether some point of time occurs within that period, I would prefer to have a dedicated Period class that contains all that logic. This approach results in a design that contains less duplication and is easier to understand.

Contrary to NHibernate, Entity Framework doesn't offer anything like this and as far as I know, doesn't plan to. NH on the other hand offers a myriad of options for creating custom types, custom collections or even composite types. Granted, you have to do a bit of digging to find the right documentation (and StackOverflow is your friend here), but if you do, it really helps to enrich your domain model.

Query flexibility
Some would argue that EF's LINQ support is much more mature, and until NH 3.2 I would have agreed. But since then, NH's LINQ support has improved substantially. For instance, during development we use an in-memory SQLite database in our query-related unit tests to make sure the query can actually be executed by NH. Before 3.2, we regularly ran into strange cast exceptions or exceptions because of unsupported expressions. Since 3.2, we've never seen those anymore.

I haven't tried to run all our existing queries against EF, but I have no doubts that it would have any issue with it. In terms of non-LINQ querying, EF supports Entity SQL as well as native SQL (although I don’t know if all vendors are supported). NHibernate offers the HQL, QueryOver and Criteria APIs next to native vendor-specific SQL. Both frameworks support stored procedures. All in all plenty of flexibility.

EF 6 uses the service locator pattern to allow replacing certain aspects of the framework at runtime. This is a good starting point for extensibility, but unfortunately the team always demonstrates this by replacing the pluralization service. As if someone would actually like to do that. Nonetheless, I'm sure the team's plan is to expose more extension points in the near future.

NH has a very extensive set of observable collections called listeners that can be used to hook into virtually every part of the framework. We've been using it for cross-cutting concerns, for hooking up auditing services and also for some CQRS related aspects. You can also tweak a lot of NH's behavior through configuration properties (although you'll have to Google…eh…Bing for the right examples).

Other notable features
Each of the frameworks has some unique features that don't fit in any of the other topics I've discussed up to now. A short summary:

  • Entity Framework 6 adds async/await support, a feature that NHibernate may never get due to the impact it has on the entire architecture.

  • It also has built-in support for automatically reconnecting to the database, which is particularly useful for a known issue with SQL Azure.

  • Both NHibernate as well as the Entity Framework support .NET 4.5, but only the latter gains some significant performance improvements from it.

  • NHibernate has a unique selling point and that is its very advanced and pluggable 2nd level cache. This has allowed significant performance improvements in one of our projects, simply by caching reference data.

  • NHibernate offers another unique feature called Futures that you can use to compose a set of queries and send them to the database as a single request.

  • The Entity Framework allows creating a DBContext with an existing open connection. As far as I know that's not possible in NHibernate.

  • Version 6 of the Entity Framework adds spatial support, something for which you need a 3rd party library to get that in NHibernate. Pedro Sousa wrote an in-depth blog post series about that.

The big difference between Entity Framework and NHibernate from a developer perspective is that the former offers an integrated set of services whereas the latter requires the combination of several open-source libraries. That on itself is not a big issue - that's why we have NuGet, don't we? - but we've noticed that those libraries are not always up-to-date soon enough when new NHibernate versions are released.

From that same perspective NHibernate does offer a lot of flexibility and clearly shows its maturity. On the other hand, you could also see that as a potential barrier for new developers. It's just much easier to get started with Entity Framework than with NH. The documentation on Entity Framework is quite comprehensive and even the new functionality for version 6 is extensively documented using feature specifications. The NHibernate documentation has always been lagging behind a bit. For instance, the new mapping system is not even mentioned even though the reference documentation mentions the correct version. The information is available, but you just have to search a bit.

The fact that the EH is being developed using a Git source control repository is also a big plus. Just look at the many pull requests they've been taking in. On the other hand, to my surprise somebody moved the NHibernate source code to GitHub while I wasn't paying attention. So on that aspect they are equals.

And does NHibernate have a future at all? Some would argue it is dead already. I don't agree though. Just look at the statistics on GitHub; 240 forks, almost 200 pull requests and a lot of commits in the last few months. I do agree that NoSQL solutions like RavenDB are extremely powerful and offer a lot of fun and flexibility for development, but the fact of the matter is that they are still not widely accepted by enterprises with a history in SQL Server or Oracle.

Nevertheless, the RAD aspect of EF cannot be ignored and is important for small short-running projects where SQL Server is the norm. And for those projects, I would wholeheartedly recommend EF. But for the bigger systems where a NoSQL solution is not an option, especially those based on Domain Driven Design, NHibernate is still the king in town.

As usual, I might have overlooked a feature or misinterpreted some aspect of both frameworks. If so, leave a comment, email me or drop me a tweet at @ddoomen.


Frans Bouma said...

> reveals that EF5 currently lacks support for DB2, Ingres, Firebird and PostgreSQL

DB2 (through their own provider), Firebird (through Firebird client), postgresql (through npgsql) all support EF.

> Version 6 of the Entity Framework adds spatial support, something for which you need a 3rd party library to get that in NHibernate.

I wouldn't bother much about that. EF maps spatial types to SQL Server through its own .NET types. But any other ORM which supports e.g. user types or type converters, uses the native .NET assembly of SQL Server. Or equivalent from e.g. postgresql.

> The NHibernate documentation has always been lagging behind a bit

Understatement of the decade. NHibernate documentation plain Sucks. With capital S.

> The information is available, but you just have to search a bit.

No, that's not 'a bit', that's actually 'a lot'. Take a look at the hbm xsd schema and then compare it to the docs. night and day.

> Entity Framework 6 adds async/await support, a feature that NHibernate may never get due to the impact it has on the entire architecture.

This isn't that bad. You can write an async wrapper around an orm core and make it async. It's not that async as EF's perhaps, but make no mistake: to have a fully async EF, you need an async aware ADO.NET provider. Only SQL server has it implemented mostly.

Kristof said...

Entity Framework has the concept of Complex Types. Is this what you mean with custom types?


Dennis Doomen said...

@Kristof, no, that is the NHibernate equivalent of a component mapping. What I mean is custom types.

Julie Lerman said...

Not trying to nit pick but FWIW, EF6 is getting mapping for private properties. I was in the design meeting where it was discussed. Here are the design meeting notes. http://entityframework.codeplex.com/wikipage?title=Design%20Meeting%20Notes%20-%20February%2020%2c%202013

Ricardo said...

I wrote a comparison myself, you can find it at http://weblogs.asp.net/ricardoperes/archive/2012/06/07/differences-between-nhibernate-and-entity-framework.aspx.
Some small corrections:
- NHibernate does automatic migrations if it is enabled. This means creating missing tables, columns, etc, automatically, and much easier than EF Migrations;
- It also supports supplying an existing connection (check connection providers).
Other than that, it's a good post!

وحيد نصيري said...

- is-nhibernate-dead?
- NH claims supports different databases, but in reality you can not query a simple from a date to date by using its SQL CE driver and dialects. just try it once.
- EntityFramework.Extended supports Futures and 2nd level caching.

jmtheblogger said...

One thing I have to add: NHibernate is just too darned hard to learn. In my case, that has definitely injured its adoption.

With EF (and a legacy db), I can Nuget a T4 template, make some tweaks, and in a few minutes, I have my model.

I came into NHibernate pumped up about the Cache and Futures -- I WANTED to switch.

Then I started dealing with the 4 (5??) different ways to write your queries, and the 4 different ways to do your mapping, and all of the config and setup, and no 1 place to go to learn it all.

I don't mind reading blog posts, but you have to be really careful because sometimes the info is dated, and there is no really good authoritative place to go for best practices, etc.

In short, NHibernate needs a Julia Lerman: http://www.amazon.com/Julia-Lerman/e/B002BMBE9Q

Unknown said...

what about comparing the two with detached context and updating a parent lots of children and a deep object graph?

I believe NH.Merge command takes the cake whereas EF forces you to write your own code.

granadaCoder said...

The lack of .Merge (for EF) should be at the top of the list for comparisons, IMHO. After you find out what you have to do with EF when in NHibernate all you did was a ".Merge", it is a deal breaker for some.

Here is some more EF love.


If you are using POCO entities without proxies, you must call the DetectChanges method to synchronize the related objects in the object context.
If you are working with disconnected objects you must manually manage the synchronization.

Mike said...

For me and DDD, I struggle with EF not being able to define eager loading in the mapping and instead having to use .Include.

With NH, I can define my Aggregate Boundaries which also allows me to use a generic IRepository<> implementation. In EF I am forced to create a repository for each AR to define the .Includes.

Mike said...

For me with regard to DDD, I find not being able to configure eager loading a pain. Instead, I have to create a repository for each aggregate root in order to put in the .Include for child entities in the aggregate. In NH, I can define the entire aggregate in configuration allowing for a more generic IRepository<> implementation

2. Mapping to private fields. This is important because I don't want to expose ICollection. Typically I have an IEnumerable with methods to add/remove so that rules can be added.