Entity Framework: Use with Caution
I've talked about the Entity Framework before and I'm certainly an advocate for it when using it makes sense, but for me it tends to be unsuitable more often than not. When does it make sense to me?
- A large project with a single developer that will be responsible for everything
- A small project with 1 - 3 developers
- A large project with a team of developers that will develop the application in horizontal slices (i.e. there will be at least one developer dedicated to the data layer)
I think #3 is really the Entity Framework's sweet spot, but outside of that I'd argue that you're better off using something like Massive or Dapper. Again, this all comes down to using the right tool for the problem at hand and how your development team operates, but that's getting a little off topic.
I recently used the Entity Framework for a project that matches the description of #2 listed above. I thought it would be a good idea to capture all of the issues that came up using the Entity Framework:
- SQL Server 2005 support is fragile unless the EDMX file is generated off of a SQL Server 2005 database instance from the start, and for all subsequent updates. If you don't, you'll find yourself manually updating the EDMX file's XML a lot.
- Explicit use of TransactionScope escalates to a Distributed Transaction on SQL Server 2005. Upgrading to Entity Framework 6.0 allowed a workaround using a Transaction object.
- UDFs are treated as query language enhancements. Manual code needs to be written to access them in your C# code. This is so you can use them in C# query statements.
- The database needs to have proper Primary and Foreign Keys. This doesn't sound like an issue, but it is if you need it to work with legacy systems that you don't control.
- A true understanding of what’s happening (i.e. when queries are run and what queries they are) is not apparent without advanced knowledge or tracing. This means that unless a developer really knows what's going on, they could be triggering lots of database calls as they dot into objects.
- Views with no defined Primary Key return the same row over and over again. The correct query gets run, unique records are transferred over the network, but the framework is trying to be "smart" about what data it hands your code... which ends up being the wrong data.
- Maintaining a customized EDMX file is a huge pain. Merging is a nightmare when multiple developers are making concurrent changes.
For the average developer the Entity Framework is a black box that magically gets you your data. This leads to two problems:
- When and where database interactions happen are not clear to the developer, which can lead to lots of odd performance problems
- If there is a problem with how the "magic" works, it involves a lot of troubleshooting and you may discover that there's no good way to fix the problem and be forced to work around it
When working on a team of full-stack developers that are also the database experts, the hoops that it forces them to jump through in order to do something that should be simple just doesn't make sense. I tend to favor simple tools that are very clear about how they operate; this results in far less surprises midway through a project.