ObjectContext.Detach Method

Removes the object from the object context.

Namespace:  System.Data.Objects
Assembly:  System.Data.Entity (in System.Data.Entity.dll)

public void Detach(
	Object entity
)

Parameters

entity
Type: System.Object

Object to be detached. Only the entity is removed; if there are any related objects that are being tracked by the same ObjectStateManager, those will not be detached automatically.

ExceptionCondition
ArgumentNullException

The entity is null.

InvalidOperationException

The entity is not associated with this ObjectContext (for example, was newly created and not associated with any context yet, or was obtained through some other context, or was already detached).

Removes the object from the ObjectStateManager. This disables change tracking and identity resolution for that object. For more information, see Attaching and Detaching Objects (Entity Framework).

After the Detach method is called, the system will no longer keep references that point to this object and it can be collected by the garbage collector.

NoteNote

Garbage collection can only occur if the user code does not have any references to the detached object.

The following considerations apply when detaching objects:

  • Detach only affects the specific object that is passed to the method. If the object being detached has related objects in the object context, those objects are not detached.

  • Detaching objects does not affect data in the data source.

  • Cascade delete directives and referential constraints are not enforced during a detach operation.

For more information, see Attaching and Detaching Objects (Entity Framework).

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

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5 SP1

.NET Framework Client Profile

Supported in: 4

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft