Gain productivity, performance and prowess with EF6 (without breaking your EF4+ or EF5 apps)

- select the contributor at the end of the page -

Entity Framework 6 (EF6) is all about putting more power at the fingertips of developers. Except for one small set of namespaces that shifted (for good reason), EF6 is backwards compatible with Entity Framework 5, 4.3, 4.1 and EF4.

The improvements to EF brought with EF6 give us several capabilities we’ve asked for through many versions of EF. Not only will you find performance benefits (whether implicit or by way of some new code) but the framework is more extensible, and developers are finding new ways to control how Entity Framework behaves. I refer to EF6 as “the Ninja edition” because it allows us to do some pretty advanced work within the API.

My course, Entity Framework 6: Ninja Edition - What's New in EF6, explores these features in great detail. Here is a high-level list of what's new in EF6, which aligns with the modules of the course:

  • Performance and Stability Improvements
  • Enhancements to the EF Tooling
  • Stored Procedure Mapping Support for Code First
  • The Return of Custom Code First Conventions
  • Many More Code First Goodies
  • Enhancements to Code First Migrations including Customization
  • Improved Database Interaction
  • Easy Database Logging
  • Code-Based DbContext Configurations and Interceptors
  • Simple methods to encapsulate frequently used logic

Developers are often wary of updating their applications to new versions of their development tools, but EF6 shouldn't give you those shivers. It works with .NET 4.0, .NET 4.5 and .NET 4.5.1, so it doesn’t require a major update for your framework. If you're currently using any version of EF4 through EF5, the only potential problem lies in explicitly using objects that exist both in .NET and EF6 (like ObjectContext). In classes where this is the case, you would need to change to the EF6 namespace. The steps are minimal and simple, and you can find them in the short MSDN Document, Upgrading to EF6.

I was told recently by a developer on Twitter that they were avoiding a move to EF6 because they're on EF 4.1 and "it IS scary to update. Gotta update .net and about six other third party libs. There is potential for huge impact.” It’s frustrating to me when developers are stymied because they haven’t found the correct information. This guy's team was under the impression that they were required to update from .NET 4 to .NET 4.5.1 in order to use EF6. When he understood that this was unnecessary and that he could easily realize the benefits of almost all of the innovations in EF that have come since EF4.1, he was eager to start investigating. His trepidation was understandable. Had he wanted to move from EF4.1 to EF5 to gain some of the EF5 benefits—most importantly a performance benefit based on a change to query caching—that would have required updating to .NET 4.5.

The changes to EF were in the core EF5 APIs that were part of the .NET Framework. However, EF6 has brought the entirety of the EF APIs into the Nuget package, entityframework.dll. (Side note: Even if you built and compiled your app on .NET 4, if it was deployed to a machine running .NET 4.5, it would still benefit from that query caching performance improvement.)

So, with one exception, all of EF’s features, including those introduced in EF5, are dependent on the framework. EF6 even enables apps using .NET 4 to benefit from its features. And it is backward compatible. While it is highly recommended to use the DbContext API for all new development, it’s possible that you have an application that uses EF4 with .NET 4, using the ObjectContext API. Even an application like this can easily shift to EF6 and take advantage of some of EF6’s new features.

While many of the new capabilities in EF6 are focused on the DbContext API or Code First, there are still a lot of EF6 additions that you can take advantage of even if you are using the ObjectContext API or the designer based EDMX. For example enum support, performance improvements to queries that use Contains or Any, smarter connection and transaction reuse. There is one feature in EF6 that does rely on .Net 4.5 — the single exception I indicated above—and that is asynchronous database interaction. .NET 4.5 introduced the Async/Await pattern. EF6 takes advantage of that with a set of async query extensions (e.g. ToListAsync) and async versions of methods such as SaveChanges (SaveChangesAsync). These can’t be used with .NET 4 because they rely on the .NET 4.5 Async/Await feature.

If you're new to EF, I recommend starting with my earlier course, Getting Started with Entity Framework 5. Hardly any of what’s in that course would change if I were to redo it just for EF6 so it is still completely relevant. After that, you can follow up with some of the more advanced courses that drill into specific features (Code First Migration, Validation) as well as all of the new features that came in EF6 with Entity Framework 6: Ninja Edition - What's New in EF6.

Get our content first. In your inbox.

Loading form...

If this message remains, it may be due to cookies being disabled or to an ad blocker.


Julie Lerman

Julie Lerman is a software coach with 30 years of experience as a developer. She is a long-time Pluralsight author, Microsoft MVP, Microsoft Regional Director and Docker Captain who lives in the hills of Vermont and continues to be known as the world’s top expert in Entity Framework. You can find Julie giving keynotes and hard-core coding sessions on Domain-Driven Design, Azure, Entity Framework and other topics at software conferences around the world. Julie has authored the highly acclaimed “Programming Entity Framework” books from O'Reilly, the MSDN Magazine Data Points column and numerous articles in your favorite programming resources.