EntityCollection(Of TEntity) Class
Represents a collection of objects on the "many" end of a relationship.
Assembly: System.Data.Entity (in System.Data.Entity.dll)
'Declaration <SerializableAttribute> _ Public NotInheritable Class EntityCollection(Of TEntity As {Class, IEntityWithRelationships}) _ Inherits RelatedEnd _ Implements ICollection(Of TEntity), IEnumerable(Of TEntity), _ IEnumerable, IListSource 'Usage Dim instance As EntityCollection(Of TEntity)
Type Parameters
- TEntity
The entity type of the collection.
An EntityCollection(Of 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(Of TEntity) is returned by a navigation property. Use the Load method to load related objects into an EntityCollection(Of TEntity). To store an unrelated collection of objects of a specific entity type, such as the result of an ObjectQuery(Of T), use an instance of the List(Of T) class.
An EntityCollection(Of TEntity) might have a corresponding EntityReference(Of TEntity). When an EntityCollection(Of TEntity) and an EntityReference(Of 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:
Creates two new SalesOrderHeader entities and adds them to the Contact entity.
Gets all related ends from the RelationshipManager that is associated with the Contact entity.
Iterates through the collection of IRelatedEnds.
Gets the EntityCollection(Of TEntity) for each related end.
Uses the Remove method to remove one of the entities from the collection.
Calls the Contains method to determine whether the object was removed from the collection.
Uses the Add method to add the entity back.
Using advWorksContext As New AdventureWorksEntities Dim contact As New Contact Dim newSalesOrder1 As New SalesOrderHeader contact.SalesOrderHeader.Add(newSalesOrder1) Dim newSalesOrder2 As New SalesOrderHeader contact.SalesOrderHeader.Add(newSalesOrder2) Dim relEnds As IEnumerable(Of IRelatedEnd) = _ CType(contact, IEntityWithRelationships) _ .RelationshipManager.GetAllRelatedEnds Dim relEnd As IRelatedEnd For Each relEnd In relEnds Dim entityCollection As EntityCollection(Of SalesOrderHeader) = _ CType(relEnd, EntityCollection(Of SalesOrderHeader)) Console.WriteLine("EntityCollection count: {0}", entityCollection.Count) entityCollection.Remove(newSalesOrder1) Dim contains As Boolean = entityCollection.Contains(newSalesOrder1) Console.WriteLine("EntityCollection count after one entity has been removed: {0}", _ entityCollection.Count) If Not contains Then Console.WriteLine("The removed entity is not in in the collection any more.") End If relEnd.Add(newSalesOrder1) Console.WriteLine("EntityCollection count after an entity has been added again: {0}", _ entityCollection.Count) Next End Using
System.Data.Objects.DataClasses.RelatedEnd
System.Data.Objects.DataClasses.EntityCollection(Of TEntity)
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.