Many to Many Relationship in Entity Framework

We are going to look at how to Configure Many to Many Relationship in Entity Framework in this tutorial. You can configure Many to Many Relationship either using Default Conventions, Data Annotations or Fluent API.

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

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. The 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.

Note that navigational property in both the classes returns the collection.

Many to Many Relationship in Code First Convention

Code First Default Convention creates the join table to handle the Many-to-Many relationship. The above model results in the creation of Join table ProjectEmployees tables. The Join Table will have primary key of the both the table

 Many To Many Relationship in Code First Convention
Many To Many Relationship in Code First Convention

Many to Many Relationship Using Data Annotations

The 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 primary key of the both the table. It will have two navigational properties one each for employee and Project class. The Employee and Project class will have the navigational property which maps to the Join Table EmployeesInProject.

The model is as shown below.

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

Many To Many Relationship in Data Annotation
Many To Many Relationship in Data Annotation

Many to Many Relationship Using Fluent API

The Many to Many Relationship can be achieved using HasMany and WithMany methods.

HasMany takes the navigational property of the employee table and sets up the Many Relationship in employees table and WithMany does the same on projects table. The Code first creates the join table employeeProjects which has primary keys from both the table.

Alternately you can define the code as follows. Only difference above and this code is that the Name of the Table generated here is ProjectEmployees.

You can change the name of the join table using the map method. The following code Creates the join table “EmployeesInProject” and also creates the Column EmployeeID and ProjectID. The Generated Schema is exactly the same as the one which was created using the data annotations.

Conclusion

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

2 thoughts on “Many to Many Relationship in Entity Framework”

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