Entity Framework Core (EF Core) has arrived and brings with it several long-awaited improvements.
Officially released this past June, EF Core is a complete rewrite of the Entity Framework. While the high-level developer APIs (DbContext, LINQ queries, etc.) feel the same, its underlying engine and associated components are lightweight, highly-modularized and very fast.
Despite its name, EF Core is not limited to the .NET Core platform. It integrates across the .NET Framework, including ASP.NET, UWP, WPF and even WinForm applications. I often show a demo that leverages EF Core with an older Web Forms application.
I could tell you that EF Core features a cleaner, concise SQL generation engine, a simple-to-use In-Memory provider and batch-driven updates and inserts, but instead, I want to focus on the number one ask from customers: Performance.
A key design goal when constructing EF Core was performance. And, as I walk you through a set of performance tests, crafted by Rowan Miller, Program Manager for Entity Framework team, I’m confident you will be highly impressed with its execution speed.
Leveraging the AdventureWorks2014 reference database, the tests execute LINQ queries against both Entity Framework 6 (the non-core version) and EF Core, repeating each test 3 times.
The first test, shown in Figure 1, executes a simple LINQ query across 19,280 rows of data in the customer table.
The test results in Figure 2 are eye-opening:
You can see that for each test iteration, EF Core delivers a 69 – 82% performance increase over EF 6. But, you’re probably thinking, “Yeah but, that’s a really simple query.”
We’ll kick up the complexity in the second test and see how EF Core fares. Figure 3 presents a test with a much more elaborate query which filters, orders and groups customer data.
Again, the test results in Figure 4 are highly impressive:
Here, we see a 77 – 82% performance increase with EF Core. Interestingly, you can see the Entity Framework query-caching capabilities kicking in and reducing execution time as we repeat the query.
Next, we move to inserting data into the database. We start by running a test where we insert 1,000 new ProductCategories, shown in Figure 5. Note that in each case, we call the SaveChanges operation once after adding the new entities to the underlying DbContext object.
The test results demonstrate a new feature in EF Core: Batch-driven Inserts and updates. In EF 6, each Insert, Delete or Update operation is a separate round-trip to the database. With EF Core, Insert, Update and Delete operations are batched. The proof lies in the outstand performance numbers, shown in Figure 6, where we see between a 96 – 98% improvement using EF Core.
Finally, we apply some EF 6 best practices when adding data: Disabling the DetectChanges feature and use of the AddRange() method. By default, each time a new ProductCategory record is added to the underlying context object, EF 6 will scan all entities in the context looking for changes by comparing the initial and current values of each.
In Figure 7, we disable change detection by setting the AutoDetectChangesEnabled property to false. Additionally, we leverage the AddRange() method which enables you to build up a list of records and submit them as a single command, also minimizing change detection.
However, even when applying EF 6 performance optimizations, the performance difference is striking as shown in Figure 8.
Development teams building new (Greenfield) applications or seeking to improve the performance of existing applications should investigate EF Core. EF Core implements most of the key features found in EF 6. Those missing features should make the cut when EF Core 2.0 releases this Summer. Take some time to investigate the EF Core roadmap at https://github.com/aspnet/EntityFramework/wiki/Roadmap. The code for the above tests can be found in Rowan Miller’s GitHub account at https://github.com/rowanmiller/Demo-EFCore.
As always, please reach out the to the Premier Developer Team for guidance, consulting or training across the entire Microsoft technology stack.
Rob Vettor is a Senior Developer Consultant with Microsoft, helping Microsoft Enterprise customers construct high-quality software. Rob focuses on Azure PaaS, including the Service Fabric and Azure App Services platforms. A former user group leader, 3-time C# MVP and co-author of the book Entity Framework 6 Recipes, Rob is a frequent presenter at technical conferences. Reach out at email@example.com.