EntityCollection<TEntity> Class

Represents a collection of objects on the "many" end of a relationship.

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

public sealed class EntityCollection<TEntity> : RelatedEnd, 
	ICollection<TEntity>, IEnumerable<TEntity>, IEnumerable, IListSource 
where TEntity : class, IEntityWithRelationships

Type Parameters


The entity type of the collection.

An EntityCollection<TEntity> is a collection of objects of a particular entity type that represents the "many" end of a one-to-many or many-to-many relationship.

An EntityCollection<TEntity> is returned by a navigation property. Use the Load method to load related objects into an EntityCollection<TEntity>. To store an unrelated collection of objects of a specific entity type, such as the result of an ObjectQuery<T>, use an instance of the List<T> class.

An EntityCollection<TEntity> might have a corresponding EntityReference<TEntity>. When an EntityCollection<TEntity> and an EntityReference<TEntity> model opposite ends of the same relationship, the integrity of the relationship is maintained at the object level. The two classes are synchronized automatically.

This class cannot be inherited.

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 Entity Data Model (Entity Framework).

This example does the following:

  1. Creates two new SalesOrderHeader entities and adds them to the Contact entity.

  2. Gets all related ends from the RelationshipManager that is associated with the Contact entity.

  3. Iterates through the collection of IRelatedEnds.

  4. Gets the EntityCollection<TEntity> for each related end.

  5. Uses the Remove method to remove one of the entities from the collection.

  6. Calls the Contains method to determine whether the object was removed from the collection.

  7. Uses the Add method to add the entity back.

using (AdventureWorksEntities advWorksContext =
    new AdventureWorksEntities())
    Contact contact = new Contact();

    // Create a new SalesOrderHeader.
    SalesOrderHeader newSalesOrder1 = new SalesOrderHeader();
    // Add SalesOrderHeader to the Contact.

    // Create another SalesOrderHeader.
    SalesOrderHeader newSalesOrder2 = new SalesOrderHeader();
    // Add SalesOrderHeader to the Contact.

    // Get all related ends
    IEnumerable<IRelatedEnd> relEnds =

    foreach (IRelatedEnd relEnd in relEnds)
        // Get Entity Collection from related end
        EntityCollection<SalesOrderHeader> entityCollection =

        Console.WriteLine("EntityCollection count: {0}",
        // Remove the first entity object.

        bool contains = entityCollection.Contains(newSalesOrder1);

        // Write the number of items after one entity has been removed
        Console.WriteLine("EntityCollection count after one entity has been removed: {0}",

        if (contains == false)
            Console.WriteLine("The removed entity is not in in the collection any more.");

        //Use IRelatedEnd to add the entity back.
        Console.WriteLine("EntityCollection count after an entity has been added again: {0}",


Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003

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

.NET Framework

Supported in: 3.5 SP1
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

© 2015 Microsoft