Deciding between NHibernate vs Entity Framework?

Sahat Yalkabov picture Sahat Yalkabov · Jul 20, 2010 · Viewed 36k times · Source

What are the main advantages and disadvantages of NHibernate and Entity Framework 4.0?

(While tagging my question, I've noticed more NHibernate tags than EF. Is NHibernate more popular?)

Answer

Peter picture Peter · Oct 28, 2019

As someone who has worked with and continues to support codebases using both NHibernate and Entity Framework I came across this old question and somewhat out-of-date answers and thought I'd post a summary update. Summary because you could write a book comparing the two in detail.

As of late 2019, both of these tools are good and either of them will do an excellent job for most people who want to use an ORM.

In 2017 I moved my team/standard use for new code from NHibernate to Entity Framework.

Why did we start with NHibernate? Back then (2013), EF was fairly new and missing features, and I felt Microsoft needed to prove they were committed to the technology. Both of those concerns have now been addressed. NHibernate over the years has disappointed with support for LINQ and Fluent configuration (we still use XML mappings). Both of those problems have now also been addressed.

So why move to Entity Framework? I decided it was time to move to entity framework because we were moving to .NET Core and it had got to the point where it was mature and being treated as a core part of .NET. As such it is updated with new features as they come out in .NET; ready with .NET core (missing some features to start with, but good now), good LINQ support, Fluent configuration the default, well integrated with ASP.NET Core, integrated with the new logging and DI frameworks, all the features we needed. I still think it was the right choice.

What to choose? My advice would be: if you are a Java developer familiar with Hibernate or porting Java code which uses Hibernate, there is little reason to move to EF, and you could move later if you need to. If you are .NET through and through, I think EF is the sensible choice, though there may be edge cases where NHibernate is better.

A common use of ORMs is to isolate from the underlying database: SQL Server, MySQL, Oracle etc: these days EF claim to support more RDBMS than NHibernate, so another tick for EF.

Being an older product with a long history (coming out of the Java ecosystem), NHibernate does have options and features which are not available in EF: HQL (Hibernate Query language, a custom query language), support for a wider range of ORM approaches, a wide range of configuration and other options, tools available. However, I haven't missed those and |I like the trimmed down feel of the latest versions of EF; it does what I need with no legacy.