다음을 통해 공유


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

이 항목에서는 관련 개체를 개체 컨텍스트에 연결하는 방법에 대한 예제를 제공합니다. 자세한 내용은 개체 연결 및 분리(Entity Framework)을 참조하십시오. 이 항목의 예제는 AdventureWorks Sales 모델을 기반으로 합니다. 이 예제의 코드를 실행하려면 프로젝트에 AdventureWorks Sales 모델을 추가하고 Entity Framework 를 사용하도록 프로젝트를 구성해야 합니다. 이렇게 하려면 방법: Entity Framework 프로젝트 수동 구성방법: 수동으로 모델 및 매핑 파일 정의(Entity Framework)의 절차를 수행합니다.

예제

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

Private Shared Sub AttachObjectGraph(ByVal currentContext As ObjectContext, ByVal detachedOrder As SalesOrderHeader, ByVal detachedItems As List(Of SalesOrderDetail))
    ' 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.SalesOrderDetails.Add(item)
    Next

    ' Attach the object graph to the supplied context. 
    currentContext.Attach(detachedOrder)
End Sub
private static void AttachObjectGraph(
    ObjectContext currentContext,
    SalesOrderHeader detachedOrder,
    List<SalesOrderDetail> detachedItems)
{
    // 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.SalesOrderDetails.Add(item);
    }

    // Attach the object graph to the supplied context.
    currentContext.Attach(detachedOrder);
}

참고 항목

개념

개체 Serialize(Entity Framework)
n 계층 응용 프로그램 작성(Entity Framework)