ObjectContext.Detach(Object) 메서드

정의

개체 컨텍스트에서 개체를 제거합니다.

public:
 void Detach(System::Object ^ entity);
public void Detach(object entity);
member this.Detach : obj -> unit
Public Sub Detach (entity As Object)

매개 변수

entity
Object

분리할 개체입니다. entity 이 항목만 제거됩니다. 동일한 ObjectStateManager개체에 의해 추적되는 관련 개체가 있는 경우 자동으로 분리되지 않습니다.

예외

entitynull입니다.

entityObjectContext 항목과 연결되지 않았습니다(예: 새로 만들어지고 아직 컨텍스트와 연결되지 않았거나, 다른 컨텍스트를 통해 가져오거나, 이미 분리된 경우).

예제

// This method is called to detach SalesOrderHeader objects and
// related SalesOrderDetail objects from the supplied object
// context when no longer needed by the application.
// Once detached, the resources can be garbage collected.
private static void DetachOrders(ObjectContext context,
    SalesOrderHeader order)
{
    try
    {
        // Detach each item from the collection.
        while (order.SalesOrderDetails.Count > 0)
        {
            // Detach the first SalesOrderDetail in the collection.
            context.Detach(order.SalesOrderDetails.First());
        }

        // Detach the order.
        context.Detach(order);
    }
    catch (InvalidOperationException ex)
    {
        Console.WriteLine(ex.ToString());
    }
}

설명

에서 개체를 ObjectStateManager제거합니다. 이렇게 하면 해당 개체에 대한 변경 내용 추적 및 ID 확인이 비활성화됩니다.

메서드가 Detach 호출된 후 시스템은 더 이상 이 개체를 가리키는 참조를 유지하지 않으며 가비지 수집기에서 수집할 수 있습니다.

메모

가비지 수집은 사용자 코드에 분리된 개체에 대한 참조가 없는 경우에만 발생할 수 있습니다.

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

  • Detach 는 메서드에 전달되는 특정 개체에만 영향을 줍니다. 분리되는 개체에 개체 컨텍스트의 관련 개체가 있는 경우 해당 개체는 분리되지 않습니다.

  • 개체 분리는 데이터 원본의 데이터에 영향을 주지 않습니다.

  • 캐스케이드 삭제 지시문 및 참조 제약 조건은 분리 작업 중에 적용되지 않습니다.

적용 대상

추가 정보