共用方式為


HOW TO:套用對中斷連結的物件所做的變更 (Entity Framework)

本主題提供的範例將示範如何將相同物件的中斷連結執行個體所做的更新套用到物件。 在遠端更新物件,並設定回伺服器來保存變更時,會使用這個程序。 如果物件只附加到伺服器上的物件內容,則當此物件已經在物件內容中時,更新會遺失或是作業會失敗。 發生這種情況是因為物件是在 Unchanged 狀態下所附加。 如需詳細資訊,請參閱附加物件 (Entity Framework)

本主題的範例是根據 Adventure Works Sales Model。 若要執行此範例中的程式碼,您必須已經將 AdventureWorks Sales Model 加入到專案中,並設定您的專案使用 Entity Framework。 若要這樣做,請完成 HOW TO:手動設定 Entity Framework 專案HOW TO:以手動方式定義 Entity Data Model (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 服務和 Entity Data Model (應用程式案例)
序列化物件 (Entity Framework)
附加物件 (Entity Framework)

其他資源

使用物件 (Entity Framework 工作)