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인 경우

엔터티 키가 잘못된 경우

예제

이 예제에서는 두 개체가 연결된 다음 관계가 정의됩니다.

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);
    }
}

설명

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

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

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

관련 개체를 연결할 때 또는 를 EntityReference<TEntity>EntityCollection<TEntity> 호출 Attach 하여 관계를 정의해야 합니다.

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

개체를 연결할 때 적용되는 고려 사항은 다음과 같습니다.

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

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

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

적용 대상

추가 정보