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.
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:
The association between the Employee and Territory entities looks as follows:
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.
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:
|