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

EF Core 5.0 brings the feature everyone has been asking for: Many-to-Many relationships without a mapped join entity. It also introduces Split Queries to fix the “Cartesian Explosion” problem in complex joins.

Many-to-Many

You can now define navigation properties on both sides, and EF Core handles the middle table.

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; }
}

// Fluent API (Optional, for custom table name)
modelBuilder.Entity<Post>()
    .HasMany(p => p.Tags)
    .WithMany(t => t.Posts)
    .UsingEntity(j => j.ToTable("PostTags"));

Split Queries

Instead of one giant SQL query with multiple JOINs (which duplicates data), Split Queries fetch related data in separate SQL calls.

var blogs = context.Blogs
    .Include(b => b.Posts)
    .AsSplitQuery() // New in 5.0
    .ToList();

Key Takeaways

  • Use **Split Queries** when loading large collections to reduce memory usage.
  • **Filtered Include** allows you to apply `Where` clauses inside `.Include()`.
  • One-to-One relationships are now strictly enforced.

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.