Entity Framework Weekly Status Updates (2020) (2024)

Expand to see status from this week...

Highlights

Many-to-many is working now in the daily builds!

We're still finishing off support, but common cases are working now. Let's walk through a simple end-to-end example.

Here are our entity types:

public class Post{ public int Id { get; set; } public string Name { get; set; } public ICollection<Tag> Tags { get; set; }}public class Tag{ public int Id { get; set; } public string Text { get; set; } public ICollection<Post> Posts { get; set; }}

Notice that Post contains a collection of Tags, and Tag contains a collection of Posts. EF Core 5.0 recognizes this as a many-to-many relationship by convention. This means no code is requied in OnModelCreating. For example, here's our DbContext:

public class BlogContext : DbContext{ public DbSet<Post> Posts { get; set; } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) => optionsBuilder .LogTo(Console.WriteLine, LogLevel.Information) .EnableSensitiveDataLogging() .UseSqlite("Data Source = test.db");}

The DbSet for Posts is all EF Core needs to discover Post and Tag and their many-to-many relationship.

When Migrations (or EnsureCreated) are used to create the database, EF Core will automatically create the join table. For example, on SQLite for this model, EF Core generates:

CREATE TABLE "Posts" ( "Id" INTEGER NOT NULL CONSTRAINT "PK_Posts" PRIMARY KEY AUTOINCREMENT, "Name" TEXT NULL);CREATE TABLE "Tag" ( "Id" INTEGER NOT NULL CONSTRAINT "PK_Tag" PRIMARY KEY AUTOINCREMENT, "Text" TEXT NULL);CREATE TABLE "PostTag" ( "Post_Id" INTEGER NOT NULL, "Tag_Id" INTEGER NOT NULL, CONSTRAINT "PK_PostTag" PRIMARY KEY ("Post_Id", "Tag_Id"), CONSTRAINT "FK_PostTag_Posts_Post_Id" FOREIGN KEY ("Post_Id") REFERENCES "Posts" ("Id") ON DELETE CASCADE, CONSTRAINT "FK_PostTag_Tag_Tag_Id" FOREIGN KEY ("Tag_Id") REFERENCES "Tag" ("Id") ON DELETE CASCADE);

(We plan to remove the underscores from the key names here.)

Let's write a small console app to save and query some entities:

public static class Program{ public static void Main() { using (var context = new BlogContext()) { context.Database.EnsureDeleted(); context.Database.EnsureCreated(); var beginnerTag = new Tag {Text = "Beginner"}; var advancedTag = new Tag {Text = "Advanced"}; var efCoreTag = new Tag {Text = "EF Core"}; context.AddRange( new Post {Name = "EF Core 101", Tags = new List<Tag> {beginnerTag, efCoreTag}}, new Post {Name = "Writing an EF database provider", Tags = new List<Tag> {advancedTag, efCoreTag}}, new Post {Name = "Savepoints in EF Core", Tags = new List<Tag> {beginnerTag, efCoreTag}}); context.SaveChanges(); } using (var context = new BlogContext()) { foreach (var post in context.Posts.Include(e => e.Tags)) { Console.Write($"Post \"{post.Name}\" has tags"); foreach (var tag in post.Tags) { Console.Write($" '{tag.Text}'"); } Console.WriteLine(); } } }}

In this example, first three Tags are created and these are added to the Post.Tags collection. Notice that the same Tag can be related to many different Posts, and vice-versa. EF Core generates the following SQL when SaveChanges is called:

INSERT INTO "Posts" ("Name")VALUES (@p0);SELECT "Id"FROM "Posts"WHERE changes() = 1 AND "rowid" = last_insert_rowid();INSERT INTO "Posts" ("Name")VALUES (@p0);SELECT "Id"FROM "Posts"WHERE changes() = 1 AND "rowid" = last_insert_rowid();INSERT INTO "Posts" ("Name")VALUES (@p0);SELECT "Id"FROM "Posts"WHERE changes() = 1 AND "rowid" = last_insert_rowid();INSERT INTO "Tag" ("Text")VALUES (@p0);SELECT "Id"FROM "Tag"WHERE changes() = 1 AND "rowid" = last_insert_rowid();INSERT INTO "Tag" ("Text")VALUES (@p0);SELECT "Id"FROM "Tag"WHERE changes() = 1 AND "rowid" = last_insert_rowid();INSERT INTO "Tag" ("Text")VALUES (@p0);SELECT "Id"FROM "Tag"WHERE changes() = 1 AND "rowid" = last_insert_rowid();INSERT INTO "PostTag" ("Post_Id", "Tag_Id")VALUES (@p1, @p2);INSERT INTO "PostTag" ("Post_Id", "Tag_Id")VALUES (@p0, @p1);INSERT INTO "PostTag" ("Post_Id", "Tag_Id")VALUES (@p0, @p1);INSERT INTO "PostTag" ("Post_Id", "Tag_Id")VALUES (@p0, @p1);INSERT INTO "PostTag" ("Post_Id", "Tag_Id")VALUES (@p0, @p1);INSERT INTO "PostTag" ("Post_Id", "Tag_Id")VALUES (@p0, @p1);

Notice that EF Core first inserts the Posts and Tags, then creates and inserts rows into the join table for the relationships.

Finally, the application creates a new DbContext and queries back the Posts and their related Tags. The SQL generated is:

Executed DbCommand (0ms) [Parameters=[], CommandType='Text', CommandTimeout='30']SELECT "p"."Id", "p"."Name", "t0"."Post_Id", "t0"."Tag_Id", "t0"."Id", "t0"."Text"FROM "Posts" AS "p"LEFT JOIN ( SELECT "p0"."Post_Id", "p0"."Tag_Id", "t"."Id", "t"."Text" FROM "PostTag" AS "p0" INNER JOIN "Tag" AS "t" ON "p0"."Tag_Id" = "t"."Id") AS "t0" ON "p"."Id" = "t0"."Post_Id"ORDER BY "p"."Id", "t0"."Post_Id", "t0"."Tag_Id", "t0"."Id"

Notice that the query automatically uses the join table to bring back all related Tags.

This demo just scratches the surface of what can be done with many-to-many relationships. For example, look for future content that shows how to introduce a join table payload while still continuing to use the relationship as many-to-many!

EF Core 5.0 preview 7

EF Core 5.0 preview 7 is available on NuGet now!

New features in preview 7 include:

Remember that all these features from previous previews are also included in preview 7:

The .NET Blog announcement has installation instructions and full details.

EF Core 3.1.6

EF Core 3.1.6 is available on NuGet now.

This is a patch release of 3.1 containing important bug fixes.

EF Core 5.0

We have completed a re-balancing of the work for EF Core 5.0. The major changes are:

  • The full implementation of many-to-many has been pulled into EF Core 5.0. (Previously we were only planning for many-to-many navigations.)
  • Reintroduction of split Includes has become a major feature for 5.0.
  • Some of the smaller, lower priority enhancements and bug fixes have been punted to allow for these changes.
  • The plan for architectural/provider documentation was too ambitious. We still believe this is important, but unfortunately it won't land with EF Core 5.0.

The plan at EF Core 5.0 plan as been updated to reflect this. Feedback is still greatly appreciated. As always, this plan is not set-in-stone; we expect it will evolve throughout the release cycle as we learn.

Each high-level theme for EF Core 5.0 is linked in the table below together with its current high-level status.

EF Core 5.0 ThemeStatus
Many-to-many navigation properties (a.k.a "skip navigations")In-progress
Full many-to-many supportIn-progress
Table-per-type (TPT) inheritance mappingDone
Filtered IncludeDone
Split IncludesDone
Rationalize ToTable, ToQuery, ToView, FromSql, etc.Done
General query enhancementsIn-progress
Migrations and deployment experienceIn-progress
EF Core platforms experienceIn-progress
PerformanceIn-progress
Architectural/contributor documentationCut
Microsoft.Data.Sqlite documentationDone
General documentationIn-progress
Fixing bugsIn-progress
Small enhancementsIn-progress

Pull requests from the last week

Community contributions

Many thanks to all our contributors!

EF Core

Burn-down for EF Core 5.0

This is the burn-down chart we use internally to track progress towards EF Core 5.0.

Entity Framework Weekly Status Updates (2020) (1)

  • The gray bar at the top represents all the issues already fixed for 5.0
    • The 'Closed' category only includes issues that have been fixed, not issues closed for any other reason.
    • The 'Fixed' category includes issues that have been fixed, but where the change has not yet been merged.
  • We're aiming to be feature-complete by the beginning of September.

Builds to use

  • The daily builds are the most up-to-date available.
  • Preview: EF Core 5.0 preview 7
    • Using the daily builds or previews is a great way to find issues and provide feedback as early as possible. The sooner we get such feedback, the more likely it will be actionable before the next official release.
  • Current and LTS: EF Core 3.1.6

Releases

See EF Core releases and planning (Roadmap) in our documentation for full details.

More Information

See the top of this issue for links to more information.

Comments are disabled on this issue to reduce noise. Please use the related discussion issue for any comments on these status updates.

Entity Framework Weekly Status Updates (2020) (2024)
Top Articles
Latest Posts
Article information

Author: Edmund Hettinger DC

Last Updated:

Views: 6711

Rating: 4.8 / 5 (78 voted)

Reviews: 85% of readers found this page helpful

Author information

Name: Edmund Hettinger DC

Birthday: 1994-08-17

Address: 2033 Gerhold Pine, Port Jocelyn, VA 12101-5654

Phone: +8524399971620

Job: Central Manufacturing Supervisor

Hobby: Jogging, Metalworking, Tai chi, Shopping, Puzzles, Rock climbing, Crocheting

Introduction: My name is Edmund Hettinger DC, I am a adventurous, colorful, gifted, determined, precious, open, colorful person who loves writing and wants to share my knowledge and understanding with you.