Announcing Entity Framework Core 2.0

Diego

August 14th, 2017

Today we are releasing the final version of Entity Framework Core 2.0, alongside .NET Core 2.0 and ASP.NET Core 2.0.

Entity Framework (EF) Core is the lightweight, extensible, and cross-platform version of Entity Framework, the popular Object/Relational Mapping (O/RM) framework for .NET.

Getting the bits

Prerequisites

In order to develop .NET Core 2.0 applications (including ASP.NET Core 2.0 applications that target .NET Core) you will need to download and install a version of the .NET Core 2.0 SDK that is appropriate to your platform. This is true even if you have installed Visual Studio 2017 version 15.3.

In order to use EF Core 2.0 or any other .NET Standard 2.0 library with a .NET platforms besides .NET Core 2.0 (e.g. with .NET Framework 4.6.1 or greater) you will need a version of NuGet that is aware of the .NET Standard 2.0 and its compatible frameworks. Here are a few ways you can obtain this:

Install Visual Studio 2017 version 15.3

If you are using Visual Studio 2015, download and upgrade NuGet client to version 3.6.0

Projects created with previous versions of Visual Studio and targeting .NET Framework may need additional modifications in order to be compatible with .NET Standard 2.0 libraries:

Edit the project file and make sure the following entry appears in the initial property group: <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>

For test projects, also make sure the following is present: <GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>

Update: There have been several issues reported on using .NET Standard 2.0 libraries on .NET Framework projects. The problems and workarounds are summarized on this .NET Standard issue.

How to install

You can start using EF Core 2.0 today by installing an EF Core 2.0-compatible database provider NuGet package in your applications. E.g. to install the SQL Server provider from the command line in a .NET Core 2.0 application:

$ dotnet add package Microsoft.EntityFrameworkCore.SqlServer -V 2.0.0

Or from the Package Manager Console in Visual Studio 2017:

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

Note that the SQL Server, SQLite, and in-memory database providers for EF Core 2.0 are already included in the ASP.NET Core 2.0 meta-package. Therefore if you are creating an ASP.NET Core 2.0 application, these steps won’t be necessary.

Check our documentation for more detailed installation and upgrade instructions as well as tutorials on using EF Core with different kinds of applications.

Update: One of the most common problem we have seen reported from customers upgrading to 2.0 is that you have to remove the [Provider].Design package (e.g. Microsoft.EntityFrameworkCore.SqlServer.Design) which has been deprecated. There are more details about this in the installation documentation page mentioned above.

What is new in this version

Here are some of the most salient new features in EF Core 2.0:

.NET Standard 2.0

EF Core now targets the new .NET Standard 2.0. The latter defines a shared surface area of over 32,000 APIs that works across .NET Framework, .NET Core, Mono, Xamarin and soon, the Universal Windows Platform. With .NET Standard 2.0, developers can reuse their code and skills on a wide range of platforms, application types and devices.

See our .NET implementation support documentation for detailed guidance on using EF Core 2.0 on each .NET implementation.

Improved LINQ translation

Queries are more efficient in EF Core 2.0 in multiple scenarios. As an example, we increased the number of patterns that can be translated to SQL, so many queries that triggered client-side evaluation in previous versions will no longer do it in 2.0.

Like query operator

You can now use EF.Functions.Like() in a LINQ query and it will be translated to LIKE in SQL or evaluated in memory if necessary. E.g. the following query:

var customers = from c in context.Customers where EF.Functions.Like(c.Name, "a%"); select c;

Is translated like this:

SELECT [c].[Id], [c].[Name] FROM [Customers] AS [c] WHERE [c].[Name] LIKE N'a%';

Owned entities and Table Splitting

You can now define “owned” or “child” entities which group properties within other entities, very similar to how complex types used to work in EF6, but with the ability to contain reference navigation properties. In combination with table splitting, owned types allow these two entities to be automatically mapped to a single Customer table:

public class Customer { public int Id { get; set; } public string Name {get; set;} public PhysicalAddress Address { get; set; } } public class PhysicalAddress { public string StreetAddress { get; set; } public Location Location { get; set; } } ... modelBuilder.Entity<Customer>() .OwnsOne(c => c.Address);

Global query filters

You can now specify filters in the model that are applied automatically to all entities of a type in all queries executed on the DbContext . E.g. given this code in OnModelCreating :

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

This query will only return posts that are not marked as deleted:

var blog = context.Blogs .Include(b => b.Posts) .FirstOrDefault(b => b.Id == id);

DbContext Pooling

Many ASP.NET Core applications can now obtain a performance boost by configuring the service registration of their DbContext types to use a pool of pre-created instances, avoiding the cost of creating new instance for every request:

services.AddDbContextPool<BloggingContext>( options => options.UseSqlServer(connectionString));

String interpolation in raw SQL methods

The following SQL query using C# string interpolation syntax now gets correctly parameterized:

var city = "Redmond"; using (var context = CreateContext()) { context.Customers.FromSql($@" SELECT * FROM Customers WHERE City = {city}"); }

This will create a parameter @p0 with a value of 'Redmond' and SQL that looks like this:

SELECT * FROM Customers WHERE City = @p0

And more

We have added a few more features such as explicitly compiled queries, self-contained entity configurations in code first and database scalar function mapping (thanks to Paul Middleton for a great contribution!), and we have fixed lots of bugs.

Please check the overview of the new features in our documentation.

Next steps

We are already hard at work on the next version of EF Core and also finishing up EF 6.2

As always, we welcome your contributions!

Many Thanks

We would like to take the chance to reiterate our gratitude to everyone contributing to our project, in particular, to external contributors who made code submissions to EF Core in 2.0. By their GitHub aliases: @BladeWise, @ErikEJ, @fitzchak, @IvanKishchenko, @laskoviymishka, @lecaillon, @MicahZoltu, @multiarc, @NickCraver, @pmiddleton, @roji, @rpawlaszek, @searus, @tinchou, @tuespetre.