ObjectContext.Attach(IEntityWithKey) 메서드

정의

개체에 엔터티 키가 있으면 개체 또는 개체 그래프를 개체 컨텍스트에 연결합니다.

public:
 void Attach(System::Data::Objects::DataClasses::IEntityWithKey ^ entity);
public void Attach (System.Data.Objects.DataClasses.IEntityWithKey entity);
member this.Attach : System.Data.Objects.DataClasses.IEntityWithKey -> unit
Public Sub Attach (entity As IEntityWithKey)

매개 변수

entity
IEntityWithKey

연결할 개체입니다.

예외

entitynull인 경우

엔터티 키가 잘못된 경우

예제

이 항목의 예제는 Microsoft SQL Server 제품 샘플: 데이터베이스를 기반으로 합니다. 이 예제에서는 두 개체가 연결된 다음 관계가 정의됩니다.

private static void AttachRelatedObjects(
    ObjectContext currentContext,
    SalesOrderHeader detachedOrder,
    List<SalesOrderDetail> detachedItems)
{
    // 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.SalesOrderDetails.Attach(item);
    }
}
Private Shared Sub AttachRelatedObjects(ByVal currentContext As ObjectContext, ByVal detachedOrder As SalesOrderHeader, ByVal detachedItems As List(Of SalesOrderDetail))
    ' 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.SalesOrderDetails.Attach(item)
    Next
End Sub

설명

개체 컨텍스트 ObjectContext 에 개체를 연결하려면 호출 Attach 합니다. 개체가 데이터 원본에 이미 있지만 현재 컨텍스트에 연결되지 않은 경우 이 작업을 수행합니다. 자세한 내용은 사용자 지정 개체합니다.

Attach 는 개체 그래프에서 개체 또는 최상위 개체를 연결하는 데 사용됩니다.

연결되는 개체는 .를 EntityKey노출하기 위해 구현 IEntityWithKey 해야 합니다. 생성된 모든 엔터티 클래스는 IEntityWithKey.

관련 개체를 연결할 때 관계를 정의하기 위해 호출 Attach EntityReference<TEntity> 하거나 EntityCollection<TEntity> 호출해야 합니다.

이 메서드는 AttachTo 메서드를 호출합니다.

다음 고려 사항은 개체를 연결할 때 적용됩니다.

  • 연결된 개체에 관련 개체가 있는 경우 해당 개체도 개체 컨텍스트에 연결됩니다.

  • 개체는 변경되지 않은 상태로 개체 컨텍스트에 추가됩니다.

  • 메서드에 전달되는 개체에는 Attach 유효한 EntityKey 값이 있어야 합니다. 개체에 유효한 EntityKey 값이 없으면 메서드를 AttachTo 사용하여 엔터티 집합의 이름을 지정합니다.

적용 대상

추가 정보