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)
Visual Basic (Declaration)
<SerializableAttribute> _
Public NotInheritable Class EntityCollection(Of TEntity As {Class, IEntityWithRelationships}) _
Inherits RelatedEnd _
Implements ICollection(Of TEntity), IEnumerable(Of TEntity), _
IEnumerable, IListSource
Dim instance As EntityCollection(Of TEntity)
[SerializableAttribute]
public sealed class EntityCollection<TEntity> : RelatedEnd,
ICollection<TEntity>, IEnumerable<TEntity>, IEnumerable, IListSource
where TEntity : class, IEntityWithRelationships
[SerializableAttribute]
generic<typename TEntity>
where TEntity : ref class, IEntityWithRelationships
public ref class EntityCollection sealed : public RelatedEnd,
ICollection<TEntity>, IEnumerable<TEntity>, IEnumerable, IListSource
JScript does not support generic types or methods.
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
using (AdventureWorksEntities advWorksContext =
new AdventureWorksEntities())
{
Contact contact = new Contact();
// Create a new SalesOrderHeader.
SalesOrderHeader newSalesOrder1 = new SalesOrderHeader();
// Add SalesOrderHeader to the Contact.
contact.SalesOrderHeader.Add(newSalesOrder1);
// Create another SalesOrderHeader.
SalesOrderHeader newSalesOrder2 = new SalesOrderHeader();
// Add SalesOrderHeader to the Contact.
contact.SalesOrderHeader.Add(newSalesOrder2);
// Get all related ends
IEnumerable<IRelatedEnd> relEnds =
((IEntityWithRelationships)contact)
.RelationshipManager.GetAllRelatedEnds();
foreach (IRelatedEnd relEnd in relEnds)
{
// Get Entity Collection from related end
EntityCollection<SalesOrderHeader> entityCollection =
(EntityCollection<SalesOrderHeader>)relEnd;
Console.WriteLine("EntityCollection count: {0}",
entityCollection.Count);
// Remove the first entity object.
entityCollection.Remove(newSalesOrder1);
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}",
entityCollection.Count);
if (contains == false)
Console.WriteLine("The removed entity is not in in the collection any more.");
//Use IRelatedEnd to add the entity back.
relEnd.Add(newSalesOrder1);
Console.WriteLine("EntityCollection count after an entity has been added again: {0}",
entityCollection.Count);
}
}
System..::.Object
System.Data.Objects.DataClasses..::.RelatedEnd
System.Data.Objects.DataClasses..::.EntityCollection<(Of <(TEntity>)>)
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
Reference
Other Resources