Data Access has been discontinued. Please refer to this page for more information.

How to: Manage Many-to-Many Associations

This article is relevant to entity models that utilize the deprecated Visual Studio integration of Telerik Data Access. The current documentation of the Data Access framework is available here.

Suppose, you have the following domain model (it is based on the Northwind database). The model includes two entities related with a many-to-many association. The Customer entity has many instance of CustomerDemographic. You can use the Customer.CustomerDemographic property to access the CustomerDemographic instances that are associated with an instance of the Customer entity. The CustomerDemographic entity has many instances of Customer. You can use the CustomerDemographic.Customer property to access the Customer instances that are associated with an instance of the CustomerDemographic entity.

In this scenario, by default the value of IsManaged for both navigation properties is False.

Below you can find how to insert and retrieve objects from domain entities related through a many-to-many association:

Additionally, depending on the configuration of the association, the IsManaged property can have the following values:

Source Property Target Property
Value Disabled Value Disabled
Both ends exist false no false no
Source End only - - false no
Target End only false yes - -

In case you need to insert a new customer with the related demographic data, you can do it like this:

  1. Set the IsManaged property of the Customer.CustomerDemographic collection to True in the Properties window and save the model.

    If you review the properties of CustomerDemographic.Customer, you will notice that its IsManaged value has changed to True too.

  2. Here is a complete code snippet showing you how to set a new relation by using the collection property. Note that it is recommended to add the new objects to the context first, and then set the relation through the collection property. The final step is to invoke the SaveChanges() method.

    using (EntitiesModel dbContext = new EntitiesModel())
    {
        Customer newCustomer = new Customer()
        {
            CustomerID = "RDSTT",
            CompanyName = "Alfreds Futterkiste",
            ContactName = "Maria Anders",
            ContactTitle = "Sales Representative",
            Address = "Obere Str. 57",
            City = "Berlin",
            PostalCode = "12209",
            Country = "Germany",
            Phone = "030-0074321",
            Fax = "030-0076545"
        };
        CustomerDemographic newCustomerDemographic = new CustomerDemographic()
        {
            CustomerTypeID = "1",
            CustomerDesc = "Regular Customer"
        };
        dbContext.Add(newCustomer);
        newCustomer.CustomerDemographics.Add(newCustomerDemographic);
        dbContext.SaveChanges();
    }
    
    Using dbContext As New EntitiesModel()
        Dim newCustomer As New Customer() With
        {
            .CustomerID = "RDSTT",
            .CompanyName = "Alfreds Futterkiste",
            .ContactName = "Maria Anders",
            .ContactTitle = "Sales Representative",
            .Address = "Obere Str. 57",
            .City = "Berlin",
            .PostalCode = "12209",
            .Country = "Germany",
            .Phone = "030-0074321",
            .Fax = "030-0076545"
        }
        Dim newCustomerDemographic As New CustomerDemographic() With
        {
            .CustomerTypeID = "1",
            .CustomerDesc = "Regular Customer"
        }
        dbContext.Add(newCustomer)
        newCustomer.CustomerDemographics.Add(newCustomerDemographic)
        dbContext.SaveChanges()
    End Using
    

If you don't set the IsManaged property to True, you may persist two new records in the database, respectively in the Customers and CustomerDemographics tables without relating them.

After the call to the SaveChanges() method both objects are saved in the database and a new row is added in the CustomerCustomerDemo table.

Note that in this case only the newCustomer is added to the context and newCustomerDemographic is added to the navigation property. This is called persistence by reachability, which means that if you build a graph of related objects, adding the top most one to the context will cause the context to track the rest too.

Since the value of IsManaged is synchronized between the two navigational properties of the association, you can achieve the same result by adding to the context the newCustomerDemographic object and adding newCustomer to the CustomerDemographic.Customer collection.

For general information about inserting objects you can check the How to: Insert Objects article.

The next example demonstrates how to retrieve all CustomerDemographic objects related to a given Customer object.

using (EntitiesModel dbContext = new EntitiesModel())
{
    Customer someCustomer = dbContext.Customers.
        FirstOrDefault(c => c.CustomerID == "SFTRT");
    if (someCustomer != null)
    {
        IList<CustomerDemographic> demographics =  
            someCustomer.CustomerDemographics;
    }
}
Using dbContext As New EntitiesModel()
    Dim someCustomer As Customer = dbContext.Customers. _
        FirstOrDefault(Function(c) c.CustomerID = "SFTRT")
    If someCustomer IsNot Nothing Then
        Dim demographics As IList(Of CustomerDemographic) =  _
            someCustomer.CustomerDemographics
    End If
End Using

For general information about retrieving objects you can check the How to: Query Data article.