デタッチされたオブジェクト対して行われた変更を適用する方法 (Entity Framework)

このトピックでは、オブジェクトのデタッチされたインスタンスに対して行われた同一オブジェクトの更新を適用する方法の例を示します。この手順は、オブジェクトがリモートで更新され、変更を維持するためにサーバーに戻されたときに使用します。オブジェクトがサーバーのオブジェクト コンテキストに単純にアタッチされた場合、そのオブジェクトが既にオブジェクト コンテキスト内にあると、更新が失われるか、操作が失敗します。これは、オブジェクトは Unchanged 状態でアタッチされるために発生します。詳細については、「オブジェクトのアタッチ (Entity Framework)」を参照してください。

このトピックの例には、Adventure Works Sales Model が使用されています。この例のコードを実行するには、あらかじめプロジェクトに AdventureWorks Sales Model を追加し、Entity Framework が使用されるようにプロジェクトを構成しておく必要があります。具体的な方法については、「Entity Framework プロジェクトを手動で構成する方法」および「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 タスク)