Many-to-Many Association

Many-to-many associations use an intermediate table that has a foreign key to the tables of both associated entities. An object uses several objects of another type, and this latter object references several objects of the first type. As is always the case for many-to-many mappings in relational databases, we need the third table which provides cross-references for the many-to-many relationship.

Many-to-many associations are supported since Entity Framework Core 5.0, they are not supported for earlier Entity Framework Core versions.

The database contains the Employees and Territories tables, and the EmployeeTerritories table, which provides cross-references for the many-to-many relationship.

images_AssociationManytoMany-table

We perform the following sequence of operations:

create an Entity Framework model;
add the Employees, Territories, and EmployeeTerritories tables to the model.

As a result, we have the following model:

images_AssociationManytoMany-model-EF

The association between the Employee and Territory entities looks as follows:

images_Association-Editor-ManyToMany-EFC

 

ExpandedToggleIcon        See Also


Send feedback on this topic

© 2008 - 2024 Devart. All rights reserved.