#100DaysOfCode - Day 23: Entity Framework (blah)

#100DaysOfCode - Day 23: Entity Framework (blah)

2 mins

It’s the weekend! And today, I got to struggle a good bit with Entity Framework. I’ve used Entity Framework almost through-out my full stack development career (starting at my first intership at Schneider Electric), and always found that it was super powerful. As I’ve started to use it more and become more aware of database complexities, as well as how complex upgrades can get, I’ve definitely started to see the limitations of this technology.

With Entity Framework, there isn’t a lot of support for migrating data between tables - a common task. Of course, you’re always able to use the Sql(@"INSERT SQL COMMAND HERE"); function to get things done, but ultimatley, this isn’t the best approach to your problems - error handling is finicky at best.

Today, I started looking into NHibernate - a port of the Java “Hibernate” class that was created in 2008 (even before Entity Framework was popularized). While it’s not something I’m necessarily looking to use (yet), I wanted to compile a list of the pros/cons between the two:

Feature EF (Classic) EF (Core) NHibernate
Async ver. 6+ ??? ver 5+
.NET Core ??? Yes ver 5.1+
LINQ ver.    
Azure Cosmos DB No WIP ???
Nuget Yes Multiple DDLS One DLL
DB Support SQL Server, … SQL Server, SQLLite SQL Server (+CE), Oracle DB, Ingres, PostreSQL, MySQL, DB2, Sybase, Informix, SQLite, Firebird, Any ODBC/OLE DB
Table Inheritance ??? Single Table (inheritance) Single Table, Class Table, and Concrete Table
PKs ??? Autognerated, Manual, sequential Sequences, High-low, Time-based Alg., Several GUID flavors, Manual
Queries** LINQ, SQL to query ” “ (more WIP) LINQ, Criteria APi, QueryOver (criteria w/ LINQ), HQL (objec-oriented SQL), SQL
Migrations / DB Generation ? Migrations API (can list all versions, go back, etc) Basic DB generation and schema update

In terms of my experience with EF, I’m most disappointed with their lack of native query support at the moment. As noted in this article, NHibernate offers a much richer feature set: “It is also possible to do updates, deletes or inserts on top of LINQ queries, which is quite nice. Also, HQL is a great language for querying the database in an agnostic way; it is similar to SQL and can even update, insert or delete entries. NHibernate can project to any class, not just anonymous types.”

Beyond query support, the article also hit the nail on the head about table inheritance: “EF Core is very easy to use, generally well documented, and this is something where NHibernate is far behind. As of now, it can only be used in relatively simple scenarios – just consider the lack of support for table inheritance strategies.”

Happy Coding!


Written on November 14, 2020