Announcing Entity Framework Core 5.0 Preview 3

Jeremy Likness

Jeremy

Today we are excited to announce the third preview release of EF Core 5.0.

The third previews of .NET 5 and ASP.NET Core 5.0 are also available now.

Prerequisites

The previews of EF Core 5.0 require .NET Standard 2.1. This means:

  • EF Core 5.0 runs on .NET Core 3.1; it does not require .NET 5.
    • This may change in future previews depending on how the plan for .NET 5 evolves.
  • EF Core 5.0 runs on other platforms that support .NET Standard 2.1.
  • EF Core 5.0 will not run on .NET Standard 2.0 platforms, including .NET Framework.

How to get EF Core 5.0 previews

EF Core is distributed exclusively as a set of NuGet packages. For example, to add the SQL Server provider to your project, you can use the following command using the dotnet tool:

dotnet add package Microsoft.EntityFrameworkCore.SqlServer --version 5.0.0-preview.3.20181.2

The EF Core packages published today are:

We have also published the 5.0 preview 3 release of the Microsoft.Data.Sqlite.Core ADO.NET provider.

Installing dotnet ef

As with EF Core 3.0 and 3.1, the dotnet ef command-line tool is no longer included in the .NET Core SDK. Before you can execute EF Core migration or scaffolding commands, you’ll have to install this package as either a global or local tool.

To install the preview tool globally, first uninstall any existing version with:

dotnet tool uninstall --global dotnet-ef

Then install with:

dotnet tool install --global dotnet-ef --version 5.0.0-preview.3.20181.2

It’s possible to use this new version of dotnet ef with projects that use older versions of the EF Core runtime.

Entity Framework Core 5.0 Preview 3
Entity Framework Core 5.0 Preview 3 CLI


What’s new in EF Core 5 Preview 3

We maintain documentation covering new features introduced into each preview.

Some of the highlights from preview 3 are called out below.

Filtered Include

The Include method now supports filtering of the entities included. For example:

var blogs = context.Blogs
    .Include(e => e.Posts.Where(p => p.Title.Contains("Cheese")))
    .ToList();

This query will return blogs together with each associated post, but only when the post title contains “Cheese”.

Skip and Take can also be used to reduce the number of included entities. For example:

var blogs = context.Blogs
    .Include(e => e.Posts.OrderByDescending(post => post.Title).Take(5)))
    .ToList();

This query will return blogs with at most five posts included on each blog.

See the Include documentation for full details.

New ModelBuilder API for navigation properties

Navigation properties are primarily configured when defining relationships. However, the new Navigation method can be used in the cases where navigation properties need additional configuration. For example, to set a backing field for the navigation when the field would not be found by convention:

modelBuilder.Entity<Blog>().Navigation(e => e.Posts).HasField("_myposts");

Note that the Navigation API does not replace relationship configuration. Instead it allows additional configuration of navigation properties in already discovered or defined relationships.

Documentation is tracked by issue #2302.

New command-line parameters for namespaces and connection strings

Migrations and scaffolding now allow namespaces to be specified on the command line. For example, to reverse engineer a database putting the context and model classes in different namespaces:

dotnet ef dbcontext scaffold "connection string" Microsoft.EntityFrameworkCore.SqlServer --context-namespace "My.Context" --namespace "My.Model"

Also, a connection string can now be passed to the database-update command:

dotnet ef database update --connection "connection string"

Equivalent parameters have also been added to the PowerShell commands used in the VS Package Manager Console.

Documentation is tracked by issue #2303.

EnableDetailedErrors has returned

For performance reasons, EF doesn’t do additional null-checks when reading values from the database. This can result in exceptions that are hard to root-cause when an unexpected null is encountered.

Using EnableDetailedErrors will add extra null checking to queries such that, for a small performance overhead, these errors are easier to trace back to a root cause.

For example:

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    => optionsBuilder
        .EnableDetailedErrors()
        .EnableSensitiveDataLogging() // Often also useful with EnableDetailedErrors 
        .UseSqlServer(Your.SqlServerConnectionString);

Documentation is tracked by issue #955.

Cosmos partition keys

The partition key to use for a given query can now be specified in the query. For example:

await context.Set<Customer>()
             .WithPartitionKey(myPartitionKey)
             .FirstAsync();

Documentation is tracked by issue #2199.

Support for the SQL Server DATALENGTH function

This can be accessed using the new EF.Functions.DataLength method. For example:

var count = context.Orders.Count(c => 100 < EF.Functions.DataLength(c.OrderDate));

Daily builds

EF Core previews are aligned with .NET 5 previews. These previews tend to lag behind the latest work on EF Core. Consider using the daily builds instead to get the most up-to-date EF Core features and bug fixes.

As with the previews, the daily builds do not require .NET 5; they can be used with GA/RTM release of .NET Core 3.1.


Documentation and feedback

EF Core docs has a new landing page! The main page for Entity Framework documentation has been overhauled to provide you with a hub experience. We hope this new format helps you find the documentation you need faster and with fewer clicks.

The starting point for all EF Core documentation is docs.microsoft.com/ef/.

Please file issues found and any other feedback on the dotnet/efcore GitHub repo.


The following short links are provided for easy reference and access.

Main documentation: https://aka.ms/efdocs

Issues and feature requests for EF Core: https://aka.ms/efcorefeedback

Entity Framework Roadmap: https://aka.ms/efroadmap

What’s new in EF Core 5.x? https://aka.ms/efcore5


Thank you from the team

A big thank you from the EF team to everyone who has used EF over the years!

ajcvickers Arthur VickersAndriySvyryd Andriy Svyryd Brice LambsonJeremyLikness Jeremy Likness
lajones lajonesmaumar Maurycy Markowskiroji Shay Rojanskysmitpatel Smit Patel

Thank you to our contributors!

A big thank you to the following community members who have already contributed code or documentation to the EF Core 5 release! (List is in chronological order of first contribution to EF Core 5).

aevitas aevitasalaatm Alaa Masoudaleksandar-manukov Aleksandar Manukovamrbadawy Amr Badawy
AnthonyMonterrosa Anthony Monterrosabbrandt Ben Brandtbenmccallum Ben McCallumccjx Clarence Cai
CGijbels Christophe Gijbelscincuranet Jiri CincuraCosto Vincent Costeldshuvaev Dmitry Shuvaev
EricStG Eric St-GeorgesErikEJ Erik Ejlskov Jensengravbox Christopher Davisivaylokenov Ivaylo Kenov
jfoshee Jacob Fosheejmzagorski Jeremy Zagorskijviau Jacob Viauknom Max K.
lohoris-crane lohoris-craneloic-sharma Loïc Sharmalokalmatador lokalmatadormariusGundersen Marius Gundersen
Marusyk Roman Marusykmatthiaslischka Matthias LischkaMaxG117 MaxG117MHDuke MHDuke
mikes-gh Mike SurcoufMuppets Neil Bostromnmichels Nícolas MichelsOOberoi Obi Oberoi
orionstudt Josh Studtozantopal Ozan Topalpmiddleton Paul Middletonprog-rajkamal Raj
ptjhuang Peter Huangralmsdeveloper Rafael Almeida Santosredoz Patrik Husfloenrmarskell Richard Marskell
sguitardude sguitardudeSimpleSamples Sam Hobbssvengeance SvenVladDragnea Vlad
vslee vsleeWeihanLi liweihanYoussef1313 Youssef Victor1iveowl 1iveowl
thomaslevesque Thomas Levesqueakovac35 Aleksander Kovačleotsarev Leonid Tsarevkostat Konstantin Triger
sungam3r Ivan Maximovdzmitry-lahoda Dzmitry LahodaLogerfo Bruno Logerfo

0 comments

Comments are closed. Login to edit/delete your existing comments