本主题提供的示例介绍如何应用对同一对象的已分离实例所做的对象更新。当对象经过远程更新并重新设置到服务器以保留更改时将使用此过程。如果对象只是简单地附加到服务器上的对象上下文中,更新将丢失;或者如果对象已在对象上下文中,操作将失败。发生这种情况是因为对象是在 Unchanged 状态下附加的。有关更多信息,请参见附加对象(实体框架)。
本主题中的示例基于 Adventure Works 销售模型。若要运行此示例中的代码,必须已将 AdventureWorks 销售模型添加到您的项目中,并将项目配置为使用 Entity Framework。为此,请完成如何:手动配置实体框架项目和如何:手动定义实体数据模型(实体框架) 中的过程。
示例
在下面的示例中,将更新的 SalesOrderDetail 对象与原始对象一起传递给 UpdateItemChanges 方法。这样无需查询对象也不必在内存中保存对象即可应用更改。
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());
        }
    }
}
本示例对原始 SalesOrderDetail 对象进行检索,然后根据传递给 UpdateItemChanges 方法的已更新 SalesOrderDetail 对象对原始对象应用更改。
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
    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());
        }
    }
}
另请参见
概念
Web 服务和实体数据模型(应用程序方案)
序列化对象(实体框架)
附加对象(实体框架)