Many to Many Relationship in Entity Framework Core

In this article, we will learn how to Configure Many to Many Relationship in Entity Framework core. You can visit the Tutorial Relationships in Entity Framework to learn how to configure one-to-one or one-to-many relationships between entities.

Configure Many-to-Many relationship

Consider the following Model of Employee and Project.

The relationship between employee and projects is many to many. The employee can be part of more than one project. The Projects can have many employees. Many to Many Relationship usually involves the creation of a join table. The join table will have a composite primary key consisting combination of the primary key of both employees and project table.

Recomended Book :Entity Framework Core in Action

Many to Many Relationship in EF Core Convention

The EF Core Convention does create the Many to Many Relationship

Many to Many Relationship Using Data Annotations

Many to Many Relationship Using Data Annotations requires you to create the Join Table in the model

The Join Table EmployeesInProject will have properties for the Foreign key from both tables. It will have two navigational properties one each for employee and Project class.

The EmployeesInProject needs a Primary Key. Currently the data annotations in EF Core does not have the option of creating Composite Primary Key.

Hence, we may have to fall back to Fluent API to create the Composite Key. In the onModelCreating method add the following code to add primary key to the EmployeesInProject

The Employee and Project entity will have the navigational property which maps to the Join Table EmployeesInProject.

The above model is mapped to the following database as shown in the image below

Many to Many Relationship Using Fluent API

In the older versions of Entity Framework automatically created join table. This feature is not supported in EF Core.

In EF Core, we must create joining entity class and then setup two one to many relationship with the joining entity.

The join table is similar to the one created in the previous section. Only change is that we are not using the ForeignKey attribute as we are creating the keys in Fluent API

In the Employee & Project models create collection navigational property EmployeesInProject as shown below.

Make EmployeeID & ProjectID as the primary key of the join tableEmployeesInProject 

Create one to many relationship between EmployeesInProject & Employee

Finally, Create the one to many relationship between EmployeesInProject & Project

Thats it.

Conclusion

In this tutorial, we looked at How to create Many to Many Relationship in Entity Framework Core using Default Convention, Data Annotations, and Fluent API

4 thoughts on “Many to Many Relationship in Entity Framework Core”

  1. I’ve tried this, but the properties Employee and Project in EmployeesInProject are empty. Both Id’s are filled. How do I fill those two properties?

    1. var employees = (from p in _context.Projects
      join ep in _context.EmployeesInProjects
      on p.ProjectId equal ep.ProjectId
      join e in _context.Employees
      on ep.EmployeeId equal e.EmployeeId
      where p.ProjectId=your_ProjectId
      select new {
      EmployeeId=e.EmployeeId,
      EmployeeName=e.EmployeeName,
      …………………………..
      }).ToList();

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.

Scroll to Top