Entity Framework Core 5.0 — Many-to-Many Relationships

Image for post
Image for post

A new feature that is released with the new version of the Entity Framework Core is the possibility to create a Many-to-Many relationship without explicitly mapping the join table. On this article, we are going to see how we can create a Many-to-Many relationship using Entity Framework Core 5.

On the previous versions of EF Core, to create a many-to-many relationship, it’s necessary to have a third entity. For example, to create a many-to-many relationship between the entity Actor and the entity Movie, it is necessary to have three entities: Actor, Movie, and ActorMovie. On the article “Entity Framework Core Relationships with Fluent API”, I explain about how you can create this kind of relationship (using EF Core 3.1) and also show examples of how you can implement it in your code, you can read clicking here.

With Entity Framework Core 5.0, it’s not necessary to have this third entity, so now it’s possible to create a Many-to-Many relationship between Actor and Movie using only these two entities. For demonstration purpose, I’ve created a Console Application using .NET Core 3.1.

Because the focus of this article is to explain about the relationship between the entities, I’m using a very simple structure, with just one layer and simples model classes, but in a real-world project, you should create the DbContext class in a separated layer. If you want to know more about how can you create the structure of your project, you can check the article “Creating an Application from Scratch using .NET Core and Angular — Part 1” clicking here.

Many-to-Many (N:N)

Let’s create a Many-to-Many relationship. We will have two entities: the Movie and the Actor. One movie can have many actors, and one actor can have many movies.

This is the Actor class:

This is the Movie class:

Note thatActor contains a collection of Movies, and Movie contains a collection of Actors. EF Core 5 recognizes this as a many-to-many relationship by convention.

This is the class that inherits from DbContext class:

Note that is not necessary to add any code in OnModelCreating, and also it’s not necessary to have a third DbSet for ActorMovies, like we need to do in the previous version of EF Core.

In the Program class, in the Main method, we have the call to the seeding method:

This is the seeding method:

On the seeding method, we are creating and associating Actor and Movie entities and this will result in join table updates happening automatically. For example, after inserting the Actors and Movies, EF will then automatically create rows in the join table.

Let’s create the migration and update the database:

Add-Migration AddedManyToManyRelationship

We can see the relationship between the tables in the SQL Server:

Then we can run the application and check the data in the database:

Image for post
Image for post

For queries, Include and other query operations work just like for any other relationship. For example, this code is in the Program class, to print the values when the application is executed:

This is the result:

Image for post
Image for post


This is an example of how we can create a Many-to-Many relationship between the entities using EF Core 5. You can check the code of this project on my GitHub:


Thanks for reading!

.NET Full-Stack Developer | C# | .NET | .NET CORE | ASP.NET MVC | Unit Test | Angular

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store