Entity Framework Core 5.0: Many-to-Many and More

Entity Framework Core 5.0 ships alongside .NET 5 and brings features that developers have missed since EF6. It completes the “missing features” gap while pushing performance further.

Many-to-Many Relationships

Finally! You no longer need to manually map the join table entity. EF Core 5 creates it for you automatically.

public class Post
{
    public int Id { get; set; }
    public ICollection<Tag> Tags { get; set; }
}

public class Tag
{
    public int Id { get; set; }
    public ICollection<Post> Posts { get; set; }
}

// EF Core 5 handles the PostTag join table automatically!

Split Queries

A common performance pitfall with EF Core 3.x was “Cartesian Explosion” when including multiple collection relationships. EF Core 5 introduces AsSplitQuery().

// Loads Blogs, then Posts, then Comments in separate SQL queries
// Consumes less memory and bandwidth than one massive JOIN
var blogs = context.Blogs
    .Include(b => b.Posts)
    .Include(b => b.Comments)
    .AsSplitQuery()
    .ToList();

Filtered Include

You can now add Where clauses inside your Include calls—a massive convenience.

var blogs = context.Blogs
    .Include(b => b.Posts.Where(p => p.IsPublished))
    .ToList();

Table-per-Type (TPT) Mapping

EF Core 5 restores TPT inheritance mapping, where each class in an inheritance hierarchy maps to its own table.

Key Takeaways

  • Many-to-many is now automatic.
  • Split Queries solve the Cartesian explosion problem.
  • Filtered Includes allow loading subsets of related data efficiently.
  • Simple logging setup via LogTo(Console.WriteLine) in OnConfiguring.

Discover more from C4: Container, Code, Cloud & Context

Subscribe to get the latest posts sent to your email.

Leave a comment

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.