EF Core 5.0 is round the corner

EF Core 5.0 is in flux and is slated to be released along with .NET 5.0. One thing to note is that EF Core 5.0 will not run on .NET Framework, unlike its predecessors. I personally think, this is good news as it is time to step up the ante and move out of the .NET legacy Framework  and onto the .NET Core ecosystem. That said, the plan is to maintain .NET Standard 2.1 compatibility through the final release.

Those of you who jumped on the EF Core bandwagon know full well that EF Core has come a long way from the time of its inception back in June 2016. Since then, there have been numerous incremental updates by the development team currently led by Arthur Wickers (formerly Diego Vega and Rowan Miller) along with the support of Andriy Svyryd, Brice Lambson, Jeremy Likness, Ia Jones, Maurycy Markowski, Shay Rojansky, Smit Patel, Erik Jensen and numerous OSS contributors around the globe who have worked diligently to completely transform this ORM. The capabilities and ease of use of EF Core has made its adoption compelling enough in modern software development ranging from desktop, web, mobile and UWP environments and across platforms.

What’s new in EF Core 5.0

  • Split Queries for related Collections

The following query will retrieve two levels of related collections using the Include like so:

var artists = context.Artists
    .Include(e => e.Albums).ThenInclude(e => e.Tags)
    .ToList();

The following SQL gets generated by default:

SELECT "a"."Id", "a"."Name", "t0"."Id", "t0"."ArtistId", "t0"."Title", "t0"."Id0", "t0"."AlbumId", "t0"."Name"
FROM "Artists" AS "a"
LEFT JOIN (
    SELECT "a0"."Id", "a0"."ArtistId", "a0"."Title", "t"."Id" AS "Id0", "t"."AlbumId", "t"."Name"
    FROM "Album" AS "a0"
    LEFT JOIN "Tag" AS "t" ON "a0"."Id" = "t"."AlbumId"
) AS "t0" ON "a"."Id" = "t0"."ArtistId"
ORDER BY "a"."Id", "t0"."Id", "t0"."Id0"

With the new AsSplitQuery API, the behavior can be altered as follows:

var artists = context.Artists
    .AsSplitQuery()
    .Include(e => e.Albums).ThenInclude(e => e.Tags)
    .ToList();

EF Core will generate the following SQL queries:

SELECT "a"."Id", "a"."Name"
FROM "Artists" AS "a"
ORDER BY "a"."Id"

SELECT "a0"."Id", "a0"."ArtistId", "a0"."Title", "a"."Id"
FROM "Artists" AS "a"
INNER JOIN "Album" AS "a0" ON "a"."Id" = "a0"."ArtistId"
ORDER BY "a"."Id", "a0"."Id"

SELECT "t"."Id", "t"."AlbumId", "t"."Name", "a"."Id", "a0"."Id"
FROM "Artists" AS "a"
INNER JOIN "Album" AS "a0" ON "a"."Id" = "a0"."ArtistId"
INNER JOIN "Tag" AS "t" ON "a0"."Id" = "t"."AlbumId"
ORDER BY "a"."Id", "a0"."Id"
  • Index Attribute
  • Improved Query Translation Exceptions
  • Exclude OnConfiguring when scaffolding
  • Translations for FirstOrDefault on strings

C#

context.Customers.Where(c => c.ContactName.FirstOrDefault() == 'A').ToList();

SQL

SELECT [c].[Id], [c].[ContactName]
FROM [Customer] AS [c]
WHERE SUBSTRING([c].[ContactName], 1, 1) = N'A'
  • Simplification of Case blocks
  • Database collations

For a detailed description of new features/announcements, please refer to the following resources below:

About Obi Oberoi

I am a Technology Aficionado who has passion for learning, speaking, reading, helping and hanging out with virtuosos!
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.