Export (0) Print
Expand All

EntityCollection<TEntity>.Attach Method (IEnumerable<TEntity>)

Defines relationships between an object and a collection of related objects in an object context.

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

public void Attach(
	IEnumerable<TEntity> entities


Type: System.Collections.Generic.IEnumerable<TEntity>

Collection of objects in the object context that are related to the source object.


entities collection is null.


The source object or an object in the entities collection is null or is not in an Unchanged or Modified state.


The relationship cannot be defined based on the EDM metadata. This can occur when the association in the conceptual schema does not support a relationship between the two types.

The Attach method is used to define relationships between an object and a collection of related objects when both the source object and the collection of related 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 collection is already filled or partially filled, the Attach method merges existing entities with the given entities. The given entities are not assumed to be the complete set of related entities.

All entities passed in must be in the Unchanged or Modified state. Objects in the Deleted state are only allowed when the state manager is already tracking the relationship instance.

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5 SP1

.NET Framework Client Profile

Supported in: 4
© 2015 Microsoft