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 a Telerik Data Access model;
make sure that the Detect Many-to-Many associations check box is selected in Model Settings;
add the Employees, Territories, and EmployeeTerritories tables to the model.

As a result, we have the following model:

images_AssociationManytoMany-model-TDA

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

images_AssociationEditorManytoManyTDA

The Territories navigation property of the Employee type is a many-to-many property to a collection of objects with the Territory type. The values for the objects in this collection can be found by looking for entries in the table for Territory objects with TerritoryID equal to the values in the TerritoryID column of the EmployeeTerritories table with the value of EmployeeID equal to this object's EmployeeID.

The Employees navigation property of the Territory type is a many-to-many property to a collection of objects with the Employee type. The values for the objects in this collection can be found by looking for entries in the table for Employees objects with EmployeeID equal to the values in the EmployeeID column of the EmployeeTerritories table with the value of TerritoryID equal to this object's TerritoryID.

 

ExpandedToggleIcon        Collection Types

The navigation property in the entity is a collection of entities from the opposite side. The type of the collection can be set globally for the model in the Model Settings using the Default collection type parameter or for each navigation property separately in the navigation property properties using the Collection Type property. By default, List collections are used. You can use the following collection for navigation properties:

Default - uses the collection type, specified in the Model Settings globally.
List
ObservableCollection
TrackedList
TrackedBindingList

 

ExpandedToggleIcon        See Also


Send feedback on this topic

© 2008 - 2024 Devart. All rights reserved.