How to: Use an EntityReference to Change Relationships Between Objects

This topic shows how to use the EntityReference object to change a relationship between two objects in the object context. When the SaveChanges method is called, the relationship change is persisted to the database as a change to the foreign key in the related table. For more information, see How to: Execute Business Logic During Association Changes.

The example in this topic is based on the Adventure Works Sales Model. To run the code in this topic, you must have already added the Adventure Works Sales Model to your project and configured your project to use the Entity Framework. For more information, see How to: Use the Entity Data Model Wizard (Entity Framework) or How to: Manually Configure an Entity Framework Project and How to: Manually Define an Entity Data Model (Entity Framework).

Example

This example shows how to use the EntityReference object to change a relationship between a SalesOrderHeader object and a related Address object that represents the shipping address for the order.

' Define the order and new address IDs. 
Dim orderId As Integer = 43669
Dim addressId As Integer = 26

Using context As New AdventureWorksEntities()
    ' Get the billing address to change to. 
    Dim address As Address = context.Addresses.Single(Function(c) c.AddressID = addressId)

    ' Get the order being changed. 
    Dim order As SalesOrderHeader = context.SalesOrderHeaders.Single(Function(o) o.SalesOrderID = orderId)

    ' You do not have to call the Load method to load the addresses for the order, 
    ' because lazy loading is set to true 
    ' by the constructor of the AdventureWorksEntities object. 
    ' With lazy loading set to true the related objects are loaded when 
    ' you access the navigation property. In this case Address. 

    ' Write the current billing street address. 
    Console.WriteLine("Current street: " & order.Address.AddressLine1)

    ' Change the billing address. 
    If Not order.Address.Equals(address) Then
        ' Use Address navigation property to change the association. 
        order.Address = address

        ' Write the changed billing street address. 
        Console.WriteLine("Changed street: " & order.Address.AddressLine1)
    End If

    ' If the address change succeeds, save the changes. 
    context.SaveChanges()

    ' Write the current billing street address. 
    Console.WriteLine("Current street: " & order.Address.AddressLine1)
End Using
// Define the order and new address IDs.
int orderId = 43669;
int addressId = 26;

using (AdventureWorksEntities context
    = new AdventureWorksEntities())
{
    // Get the billing address to change to.
    Address address =
        context.Addresses.Single(c => c.AddressID == addressId);

    // Get the order being changed.
    SalesOrderHeader order =
        context.SalesOrderHeaders.Single(o => o.SalesOrderID == orderId);

    // You do not have to call the Load method to load the addresses for the order,
    // because  lazy loading is set to true 
    // by the constructor of the AdventureWorksEntities object. 
    // With  lazy loading set to true the related objects are loaded when
    // you access the navigation property. In this case Address.

    // Write the current billing street address.
    Console.WriteLine("Current street: "
        + order.Address.AddressLine1);

    // Change the billing address.
    if (!order.Address.Equals(address))
    {
        // Use Address navigation property to change the association.
        order.Address = address;

        // Write the changed billing street address.
        Console.WriteLine("Changed street: "
            + order.Address.AddressLine1);
    }

    // If the address change succeeds, save the changes.
    context.SaveChanges();

    // Write the current billing street address.
    Console.WriteLine("Current street: "
        + order.Address.AddressLine1);
}

See Also

Tasks

How to: Use the Foreign Key Property to Change Relationships Between Objects
How to: Execute Business Logic During Association Changes

Concepts

Working with Objects
Defining and Managing Relationships