Export (0) Print
Expand All

EntityCollection<TEntity>.Attach Method (TEntity)

Defines a relationship between two attached objects in an object context.

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

public void Attach(
	TEntity entity
)

Parameters

entity
Type: TEntity

The object being attached.

ExceptionCondition
ArgumentNullException

When the entity is null.

InvalidOperationException

When the entity cannot be related to the source object. This can occur when the association in the conceptual schema does not support a relationship between the two types.

-or-

When either object is null or is not in an Unchanged or Modified state.

The Attach method is used to define relationships between two objects when both objects already exist in the object context. To attach an object or an object graph where the relationships are already defined, call the Attach method on the ObjectContext. To create a new object that is related to the source object, call the Add method on the EntityCollection<TEntity>. For more information, see Attaching Objects (Entity Framework).

If the EntityCollection<TEntity> already has loaded objects, the Attach method merges the object together with the existing objects in the EntityCollection<TEntity>.

The attached object is not assumed to be the complete set of related entity objects.

The object associated with this EntityCollection<TEntity> and all objects being attached to it must be in an Unchanged or Modified state.

Objects in the Deleted state can only be attached when the ObjectStateManager is already tracking the relationship instance.

This example is based on the Adventure Works Sales Model. To run the code in this example, you must have already added the AdventureWorks Sales Model to your project and configured your project to use the Entity Framework. To do this, complete the procedures in How to: Manually Configure an Entity Framework Project and How to: Manually Define an E*ntity Data Model (Entity Framework).

This example attaches a collection of detached SalesOrderDetail objects and a detached SalesOrderHeader object to an object context, and then defines the relationships between the SalesOrderHeader object and each SalesOrderDetail object.

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

.NET Framework

Supported in: 4.5.2, 4.5.1, 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.

Show:
© 2014 Microsoft