방법: 관련 개체 연결(Entity Framework)

이 항목에서는 관련 개체를 개체 컨텍스트에 연결하는 방법에 대한 예제를 제공합니다. 자세한 내용은 개체 연결(Entity Framework)을 참조하십시오. 이 절차는 XML serialization을 사용하여 serialize된 개체 그래프를 다시 생성할 때 사용됩니다.

이 항목의 예제는 Adventure Works Sales 모델을 기반으로 합니다. 이 예제의 코드를 실행하려면 프로젝트에 AdventureWorks Sales 모델을 추가하고 프로젝트에서 엔터티 프레임워크를 사용하도록 구성해야 합니다. 이렇게 하려면 방법: Entity Framework 프로젝트 수동 구성방법: 엔터티 데이터 모델 수동 정의(Entity Framework)의 절차를 수행합니다.

예제

이 예제에서는 분리된 SalesOrderDetail 개체와 분리된 SalesOrderHeader 개체의 컬렉션을 개체 컨텍스트에 연결한 다음 SalesOrderHeader 개체와 각 SalesOrderDetail 개체 사이에 관계를 정의합니다.

Private Shared Sub AttachRelatedObjects( _
ByVal currentContext As ObjectContext, _
ByVal detachedOrder As SalesOrderHeader, _
ByVal detachedItems As List(Of SalesOrderDetail))
    Try
        ' Attach the root detachedOrder object to the supplied context.
        currentContext.Attach(detachedOrder)

        ' Attach each detachedItem to the context, and define each relationship
        ' by attaching the attached SalesOrderDetail object to the EntityCollection on 
        ' the SalesOrderDetail navigation property of the now attached detachedOrder.
        For Each item As SalesOrderDetail In detachedItems
            currentContext.Attach(item)
            detachedOrder.SalesOrderDetail.Attach(item)
        Next

    Catch ex As InvalidOperationException
        Console.WriteLine(ex.ToString())
    End Try
End Sub
private static void AttachRelatedObjects(
    ObjectContext currentContext,
    SalesOrderHeader detachedOrder,
    List<SalesOrderDetail> detachedItems)
{
    try
    {
        // Attach the root detachedOrder object to the supplied context.
        currentContext.Attach(detachedOrder);

        // Attach each detachedItem to the context, and define each relationship
        // by attaching the attached SalesOrderDetail object to the EntityCollection on 
        // the SalesOrderDetail navigation property of the now attached detachedOrder.
        foreach (SalesOrderDetail item in detachedItems)
        {
            currentContext.Attach(item);
            detachedOrder.SalesOrderDetail.Attach(item);
        }
    }
    catch (InvalidOperationException ex)
    {
        Console.WriteLine(ex.GetType().ToString() + ": " + ex.ToString());
    }
}

이 예제에서는 분리된 SalesOrderDetail 개체의 컬렉션을 분리된 SalesOrderHeader 개체에 연결한 다음 이 개체 그래프를 개체 컨텍스트에 연결합니다.

Private Shared Sub AttachObjectGraph( _
ByVal currentContext As ObjectContext, _
ByVal detachedOrder As SalesOrderHeader, _
ByVal detachedItems As List(Of SalesOrderDetail))
    Try
        ' Define the relationships by adding each SalesOrderDetail 
        ' object in the detachedItems List<SalesOrderDetail> collection to the 
        ' EntityCollection on the SalesOrderDetail navigation property of detachedOrder.
        For Each item As SalesOrderDetail In detachedItems
            detachedOrder.SalesOrderDetail.Add(item)
        Next

        ' Attach the object graph to the supplied context.
        currentContext.Attach(detachedOrder)

    Catch ex As InvalidOperationException
        Console.WriteLine(ex.ToString())
    End Try
End Sub
private static void AttachObjectGraph(
    ObjectContext currentContext,
    SalesOrderHeader detachedOrder, 
    List<SalesOrderDetail> detachedItems)
{
        try
        {
            // Define the relationships by adding each SalesOrderDetail 
            // object in the detachedItems List<SalesOrderDetail> collection to the 
            // EntityCollection on the SalesOrderDetail navigation property of detachedOrder.
            foreach (SalesOrderDetail item in detachedItems)
            {
                detachedOrder.SalesOrderDetail.Add(item);
            }

            // Attach the object graph to the supplied context.
            currentContext.Attach(detachedOrder);
        }
        catch (InvalidOperationException ex)
        {
            Console.WriteLine(ex.GetType().ToString() + ": " + ex.ToString());
        }
}

참고 항목

개념

개체 Serialize(Entity Framework)
웹 서비스 및 엔터티 데이터 모델(응용 프로그램 시나리오)

기타 리소스

개체 컨텍스트 관리(Entity Framework)
개체 사용(Entity Framework 작업)