Export (0) Print
Expand All

ObjectContext.ApplyPropertyChanges Method

Applies property changes from a detached object to an object already attached to the object context.

Namespace:  System.Data.Objects
Assembly:  System.Data.Entity (in System.Data.Entity.dll)

public void ApplyPropertyChanges(
	string entitySetName,
	Object changed
)

Parameters

entitySetName
Type: System.String

The name of the entity set to which the object belongs.

changed
Type: System.Object

The detached object that has property updates to apply to the original object.

ExceptionCondition
ArgumentNullException

When entitySetName is null or an empty string.

-or-

When changed is null.

InvalidOperationException

When the EntitySet from entitySetName does not match the EntitySet of the object’s EntityKey.

-or-

When the entity is in a state other than Modified or Unchanged.

-or-

The original object is not attached to the context.

ArgumentException

When the type of the changed object is not the same type as the original object.

The ApplyPropertyChanges method is used to apply property changes from a modified, detached version of an object to the original version attached to the ObjectContext. This supports scenarios, such as Web services, where an entity object is detached and sent to a remote application where property updates are made. This method enables you to more easily apply these changes back to the original object.

After ApplyPropertyChanges, the object is in the Modified state. You must call the SaveChanges method to update the data source.

The original object must exist in the ObjectStateManager and must be in the Modified or Unchanged state. The original object is only modified if there are modified properties in the changed object.

The EntityKey property of the supplied object must be set to a valid EntityKey.

ApplyPropertyChanges does not affect navigation properties or related objects.

ApplyPropertyChanges only sets properties that exist in the entity metadata for the type. For example, properties that are added in a partial class are not included in the ApplyPropertyChanges operation.

The example in this topic is based on the Adventure Works Sales Model. In this example, an updated SalesOrderDetail object is passed to the UpdateItemChanges method, together with the original object. This enables changes to be applied without querying for the object or having to persist it in memory.

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 that is passed to the UpdateItemChanges method.

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());
        }
    }
}

Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5 SP1

Community Additions

ADD
Show:
© 2014 Microsoft