New Capabilities in Entity Framework Core 2.0

Monday Sep 11th 2017 by Joydip Kanjilal
Share:

Take advantage of Entity Framework Core 2.0 to build applications that can target a wide variety of platforms and devices.

Entity Framework has for quite some time been Microsoft's flagship ORM and is quite popular within the Microsoft development community. It is an extended object relational mapping (ORM) tool from Microsoft that has become increasingly popular over the past few years. The primary goal of the Entity Framework was to raise the level of abstraction and simplify development of data-aware applications with reduced effort and reduced KLOC (thousands of lines of code).

Entity Framework has been updated over the last few years and is now a mature ORM. With the advent of .NET Core, Microsoft also has come up with a version of this ORM that is compliant with the .NET Core platform. This is what we call Entity Framework Core; the most recent version of it is Entity Framework 2.0. This article presents a discussion on all the new features and enhancements in Entity Framework Core 2.0.

Pre-requisites

To run .NET Core 2.0 applications in your system, you should have the .NET Core 2.0 SDK installed in your system. You can download a copy of .NET Core 2.0 SDK from this link.

If you want to work with Entity Framework Core 2.0, besides having .NET Core 2.0 installed, you also should have Visual Studio 2015/2017 installed in your system. To install Entity Framework Core 2.0, you can take advantage of the Package Manager Console Window within the Visual Studio IDE. Here's the command you can use to install Entity Framework Core 2.0:

PM> Install-Package Microsoft.EntityFrameworkCore.SqlServer
   -Version 2.0.0

It should be noted that SQLServer, SQLLite, and the in-memory database providers for Entity Framework Core 2.0 are already available as part of ASP.NET Core 2.0. So, you would not have to install the Entity Framework Core 2.0 database provider via NuGet if you are using ASP.NET Core 2.0.

So, What's New in Entity Framework Core 2.0?

This section lists the new features and enhancements. Entity Framework Core 2.0 can be used with .NET Standard 2.0. This implies that you now can target a wide variety of platforms, devices, and applications. Some of the existing new features and enhancements in Entity Framework Core 2 include the following:

  • Support for the Like Query Operator
  • Support for Global Query Filters
  • Support for DbContext Pooling
  • Support for Table Splitting

Support for the Like Query Operator

You now can take advantage of Like() in a LINQ query when working with Entity Framework Core 2.0. Here is an example:

var authors =
   from a in context.Authors
   where EF.Functions.Like(c.FirstName, "J%");
   select a;

Support for Global Query Filters

Support for Global Query filters is another new feature added to Entity Framework Core 2.0. This feature allows you to specify filters in the model so that they will be automatically applied to all the entities of a particular type. Here is an example that illustrates how this can be used:

modelBuilder.Entity<Author>()
   .HasQueryFilter(a => !p.IsDeleted);

When the preceding is applied as a Global Query filter, the following code snippet can be used to retrieve all author records that are not deleted.

var result = context.Authors
   .Include(a => a.Authors)
   .FirstOrDefault(a => a.Id == id);

Support for DbContext Pooling

Support for DbContext pooling is a feature that you can take advantage of to improve performance. This feature enables you to configure the service registration of the DbContext instance so that you can re-use the pre-created instances in lieu of creating them again and again. In other words, DbContext pooling is a feature that allows you to configure service registration such that you can achieve a performance boost by re-using a pool of pre-created instances. The following code snippet illustrates how this can be used:

services.AddDbContextPool<AuthorsContext>(
   options => options.UseSqlServer(dbConnectionString));

Support for Table Splitting

With Entity Framework 2.0, you can write more efficient queries and defined child entities that can group properties within entities. In essence, table splitting enables you to map two or more entity types to the same table with the primary key column(s) being shared.

Summary

Entity Framework Core 2.0 is the latest version of Microsoft's flagship ORM that is targeted at the .NET Core Framework. This article presented an overview on the new capabilities in Entity Framework Core 2.0 with relevant code examples. Happy reading!

Share:
Home
Mobile Site | Full Site
Copyright 2017 © QuinStreet Inc. All Rights Reserved