Share via


How to: Apply Changes Made to a Detached Object (Entity Framework)

This topic provides an example of how to apply to an object updates made to a detached instance of the same object. This procedure is used when an object is updated remotely and set back to the server to persist the changes. If the object were simply attached to an object context at the server, updates would be lost or the operation would fail if the object was already in the object context. This occurs because objects are attached in an Unchanged state. For more information, see Attaching Objects (Entity Framework).

The example in this topic is based on the Adventure Works Sales Model. To run the code in this example, you must have already added the AdventureWorks Sales Model to your project and configured your project to use the Entity Framework. To do this, complete the procedures in How to: Manually Configure an Entity Framework Project and How to: Manually Define an Entity Data Model (Entity Framework).

Example

In the following example, an updated SalesOrderDetail object is passed to the UpdateItemChanges method, along with the original object. This enables changes to be applied without querying for the object or having to persist it in memory.

Private Shared Sub ApplyItemUpdates(ByVal originalItem As SalesOrderDetail, _
    ByVal updatedItem As SalesOrderDetail)
    Using advWorksContext As AdventureWorksEntities = _
        New AdventureWorksEntities()
        Try

            ' Attach the original item to the object context.
            advWorksContext.Attach(originalItem)

            ' Call the ApplyPropertyChanges method to apply changes
            ' from the updated item to the original version.
            advWorksContext.ApplyPropertyChanges( _
                "SalesOrderDetail", updatedItem)

            advWorksContext.SaveChanges()

        Catch ex As InvalidOperationException
            Console.WriteLine(ex.ToString())
        End Try
    End Using
End Sub
private static void ApplyItemUpdates(SalesOrderDetail originalItem,
    SalesOrderDetail updatedItem)
{
    using (AdventureWorksEntities advWorksContext = 
        new AdventureWorksEntities())
    {
        try
        {
            // Attach the original item to the object context.
            advWorksContext.Attach(originalItem);

            // Call the ApplyPropertyChanges method to apply changes
            // from the updated item to the original version.
            advWorksContext.ApplyPropertyChanges("SalesOrderDetail",
                updatedItem);

            advWorksContext.SaveChanges();
        }
        catch (InvalidOperationException ex)
        {
            Console.WriteLine(ex.ToString());
        }
    }
}

In this example, the original SalesOrderDetail object is retrieved and then changes are applied to it based on an updated SalesOrderDetail object passed to the UpdateItemChanges method.

Private Shared Sub ApplyItemUpdates(ByVal updatedItem As SalesOrderDetail)
    ' Define an ObjectStateEntry and EntityKey for the current object.
    Dim key As EntityKey
    Dim originalItem As Object = Nothing

    Using advWorksContext As AdventureWorksEntities = _
        New AdventureWorksEntities()
        Try
            ' Create the detached object's entity key.
            key = advWorksContext.CreateEntityKey("SalesOrderDetail", updatedItem)

            ' Get the original item based on the entity key from the context
            ' or from the database.
            If advWorksContext.TryGetObjectByKey(key, originalItem) Then
                ' Call the ApplyPropertyChanges method to apply changes
                ' from the updated item to the original version.
                advWorksContext.ApplyPropertyChanges( _
                    key.EntitySetName, _
                    updatedItem)
            End If

            advWorksContext.SaveChanges()
        Catch ex As InvalidOperationException
            Console.WriteLine(ex.ToString())
        End Try

    End Using
End Sub
private static void ApplyItemUpdates(SalesOrderDetail updatedItem)
{
    // Define an ObjectStateEntry and EntityKey for the current object.
    EntityKey key;
    object originalItem;

    using (AdventureWorksEntities advWorksContext =
        new AdventureWorksEntities())
    {
        try
        {
            // Create the detached object's entity key.
            key = advWorksContext.CreateEntityKey("SalesOrderDetail", updatedItem);
            
            // Get the original item based on the entity key from the context
            // or from the database.
            if (advWorksContext.TryGetObjectByKey(key, out originalItem))
            {
                // Call the ApplyPropertyChanges method to apply changes
                // from the updated item to the original version.
                advWorksContext.ApplyPropertyChanges(
                    key.EntitySetName, updatedItem);
            }

            advWorksContext.SaveChanges();
        }
        catch (InvalidOperationException ex)
        {
            Console.WriteLine(ex.ToString());
        }
    }
}

See Also

Concepts

Web Services and the Entity Data Model (Application Scenarios)
Serializing Objects (Entity Framework)
Attaching Objects (Entity Framework)

Other Resources

Working with Objects (Entity Framework Tasks)