A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://learn.microsoft.com/en-us/dotnet/api/system.data.objects.objectcontext.detach below:

ObjectContext.Detach(Object) Method (System.Data.Objects) | Microsoft Learn

ObjectContext.Detach(Object) Method Definition

Removes the object from the object context.

public:
 void Detach(System::Object ^ entity);
public void Detach(object entity);
member this.Detach : obj -> unit
Public Sub Detach (entity As Object)
Parameters
entity
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.

Exceptions

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).

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

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

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.

Note

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:

For more information, see Attaching and Detaching Objects.

See also

Collaborate with us on GitHub

The source for this content can be found on GitHub, where you can also create and review issues and pull requests. For more information, see our contributor guide. In this article

Was this page helpful?


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4