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.

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-EF

Though there is no entity for the EmployeesTerritories join table in the conceptual part, the storage part contains a table and constraints corresponding to it.

The Association Mapping Details dialog shows how the EmployeesTerritories linking table is mapped to the association ends:

images_Association-Mapping-Details-MtoM-EF

ExpandedToggleIcon        See Also


Send feedback on this topic

© 2008 - 2024 Devart. All rights reserved.