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

entity가 이 ObjectContext와 연결되어 있지 않은 경우(예: 새로 만들어진 후 컨텍스트에 아직 연결되지 않았거나, 다른 컨텍스트를 통해 가져왔거나, 이미 분리된 경우)

예제

// 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 는 메서드에 전달되는 특정 개체에만 영향을 줍니다. 분리되는 개체에 관련 개체가 개체 컨텍스트에 있는 경우 해당 개체는 분리되지 않습니다.

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

  • 계단식 삭제 지시문 및 참조 제약 조건은 분리 작업 중에 적용되지 않습니다.

자세한 내용은 개체 연결 및 분리를 참조하세요.

적용 대상

추가 정보