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

설명

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

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

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

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

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

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

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

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

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

적용 대상

추가 정보