EntityCollection<TEntity> Class
Definition
Represents a collection of objects on the "many" end of a relationship.
generic <typename TEntity>
where TEntity : classpublic ref class EntityCollection sealed : System::Data::Objects::DataClasses::RelatedEnd, System::Collections::Generic::ICollection<TEntity>, System::Collections::Generic::IEnumerable<TEntity>, System::ComponentModel::IListSource
[System.Serializable]
public sealed class EntityCollection<TEntity> : System.Data.Objects.DataClasses.RelatedEnd, System.Collections.Generic.ICollection<TEntity>, System.Collections.Generic.IEnumerable<TEntity>, System.ComponentModel.IListSource where TEntity : class
type EntityCollection<'Entity (requires 'Entity : null)> = class
inherit RelatedEnd
interface ICollection<'Entity (requires 'Entity : null)>
interface seq<'Entity (requires 'Entity : null)>
interface IEnumerable
interface IListSource
Public NotInheritable Class EntityCollection(Of TEntity)
Inherits RelatedEnd
Implements ICollection(Of TEntity), IEnumerable(Of TEntity), IListSource
Type Parameters
- TEntity
The entity type of the collection.
- Inheritance
- Attributes
- Implements
Examples
This example is based on the . 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 the Model and Mapping Files.
This example does the following:
Creates two new
SalesOrderHeader
entities and adds them to theContact
entity.Gets all related ends from the RelationshipManager that is associated with the
Contact
entity.Iterates through the collection of IRelatedEnds.
Gets the EntityCollection<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 (AdventureWorksEntities context =
new AdventureWorksEntities())
{
Contact contact = new Contact();
// Create a new SalesOrderHeader.
SalesOrderHeader newSalesOrder1 = new SalesOrderHeader();
// Add SalesOrderHeader to the Contact.
contact.SalesOrderHeaders.Add(newSalesOrder1);
// Create another SalesOrderHeader.
SalesOrderHeader newSalesOrder2 = new SalesOrderHeader();
// Add SalesOrderHeader to the Contact.
contact.SalesOrderHeaders.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);
}
}
Using context As New AdventureWorksEntities()
Dim contact As New Contact()
' Create a new SalesOrderHeader.
Dim newSalesOrder1 As New SalesOrderHeader()
' Add SalesOrderHeader to the Contact.
contact.SalesOrderHeaders.Add(newSalesOrder1)
' Create another SalesOrderHeader.
Dim newSalesOrder2 As New SalesOrderHeader()
' Add SalesOrderHeader to the Contact.
contact.SalesOrderHeaders.Add(newSalesOrder2)
' Get all related ends
Dim relEnds As IEnumerable(Of IRelatedEnd) = DirectCast(contact, IEntityWithRelationships).RelationshipManager.GetAllRelatedEnds()
For Each relEnd As IRelatedEnd In relEnds
' Get Entity Collection from related end
Dim entityCollection As EntityCollection(Of SalesOrderHeader) = DirectCast(relEnd, EntityCollection(Of SalesOrderHeader))
Console.WriteLine("EntityCollection count: {0}", entityCollection.Count)
' Remove the first entity object.
entityCollection.Remove(newSalesOrder1)
Dim contains As Boolean = 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 Then
Console.WriteLine("The removed entity is not in in the collection any more.")
End If
'Use IRelatedEnd to add the entity back.
relEnd.Add(newSalesOrder1)
Console.WriteLine("EntityCollection count after an entity has been added again: {0}", entityCollection.Count)
Next
End Using
Remarks
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.
Constructors
EntityCollection<TEntity>() |
Initializes a new instance of the EntityCollection<TEntity> class. |
Properties
Count |
Gets the number of objects that are contained in the collection. |
IsLoaded |
Gets a value that indicates whether all related objects have been loaded. (Inherited from RelatedEnd) |
IsReadOnly |
Gets a value that indicates whether the EntityCollection<TEntity> is read-only. |
RelationshipName |
Gets the name of the relationship in which this related end participates. (Inherited from RelatedEnd) |
RelationshipSet |
Gets a reference to the metadata for the related end. (Inherited from RelatedEnd) |
SourceRoleName |
Gets the role name at the source end of the relationship. (Inherited from RelatedEnd) |
TargetRoleName |
Gets the role name at the target end of the relationship. (Inherited from RelatedEnd) |
Methods
Add(TEntity) |
Adds an object to the collection. |
Attach(IEnumerable<TEntity>) |
Defines relationships between an object and a collection of related objects in an object context. |
Attach(TEntity) |
Defines a relationship between two attached objects in an object context. |
Attach<TEntity>(IEnumerable<TEntity>, Boolean) |
Defines a relationship between two attached objects. (Inherited from RelatedEnd) |
Clear() |
Removes all entities from the collection. |
Contains(TEntity) |
Determines whether a specific object exists in the collection. |
CopyTo(TEntity[], Int32) |
Copies all the contents of the collection to an array, starting at the specified index of the target array. |
CreateSourceQuery() |
Returns an object query that, when it is executed, returns the same set of objects that exists in the current collection. |
Equals(Object) |
Determines whether the specified object is equal to the current object. (Inherited from Object) |
GetEnumerator() |
Returns an enumerator that is used to iterate through the objects in the collection. |
GetHashCode() |
Serves as the default hash function. (Inherited from Object) |
GetType() |
Gets the Type of the current instance. (Inherited from Object) |
Load() |
When overridden in a derived class, loads the related object or objects into the related end with the default merge option. (Inherited from RelatedEnd) |
Load(MergeOption) |
Loads related objects into the collection, using the specified merge option. |
MemberwiseClone() |
Creates a shallow copy of the current Object. (Inherited from Object) |
Merge<TEntity>(IEnumerable<TEntity>, MergeOption, Boolean) |
Merges related entities into the local EntityCollection<TEntity>. (Inherited from RelatedEnd) |
OnCollectionDeserialized(StreamingContext) |
Used internally to deserialize entity objects. |
OnDeserialized(StreamingContext) |
Used internally to deserialize entity objects along with the RelationshipManager instances. (Inherited from RelatedEnd) |
OnSerializing(StreamingContext) |
Used internally to serialize entity objects. |
Remove(TEntity) |
Removes an object from the collection and marks the relationship for deletion. |
ToString() |
Returns a string that represents the current object. (Inherited from Object) |
ValidateEntityForAttach<TEntity>(TEntity, Int32, Boolean) |
Determines whether an object can be attached to the local collection or reference. (Inherited from RelatedEnd) |
ValidateLoad<TEntity>(MergeOption, String) |
Ensures that the related object or objects can be successfully loaded into the local collection or reference. (Inherited from RelatedEnd) |
ValidateOwnerForAttach() |
Ensures that the object to which the related end belongs supports an attach operation. (Inherited from RelatedEnd) |
Events
AssociationChanged |
Occurs when a change is made to a related end. (Inherited from RelatedEnd) |
Explicit Interface Implementations
IEnumerable.GetEnumerator() |
Returns an enumerator that is used to iterate through the set of values cached by EntityCollection<TEntity>. |
IListSource.ContainsListCollection |
Gets a value that indicates whether the related entity collection consists of collection objects. |
IListSource.GetList() |
Returns the collection as an IList used for data binding. |
IRelatedEnd.Add(IEntityWithRelationships) |
Adds an object to the related end. (Inherited from RelatedEnd) |
IRelatedEnd.Add(Object) |
Adds an object to the related end. (Inherited from RelatedEnd) |
IRelatedEnd.Attach(IEntityWithRelationships) |
Attaches an object at the related end. (Inherited from RelatedEnd) |
IRelatedEnd.Attach(Object) |
Attaches an object to the related end. (Inherited from RelatedEnd) |
IRelatedEnd.CreateSourceQuery() |
Returns an IEnumerable that represents the objects that belong to the related end. (Inherited from RelatedEnd) |
IRelatedEnd.Remove(IEntityWithRelationships) |
Removes an object from the collection at the related end. (Inherited from RelatedEnd) |
IRelatedEnd.Remove(Object) |
Removes an object from the related end if the object is part of the related end. (Inherited from RelatedEnd) |
Extension Methods
CopyToDataTable<T>(IEnumerable<T>) |
Returns a DataTable that contains copies of the DataRow objects, given an input IEnumerable<T> object where the generic parameter |
CopyToDataTable<T>(IEnumerable<T>, DataTable, LoadOption) |
Copies DataRow objects to the specified DataTable, given an input IEnumerable<T> object where the generic parameter |
CopyToDataTable<T>(IEnumerable<T>, DataTable, LoadOption, FillErrorEventHandler) |
Copies DataRow objects to the specified DataTable, given an input IEnumerable<T> object where the generic parameter |
Cast<TResult>(IEnumerable) |
Casts the elements of an IEnumerable to the specified type. |
OfType<TResult>(IEnumerable) |
Filters the elements of an IEnumerable based on a specified type. |
AsParallel(IEnumerable) |
Enables parallelization of a query. |
AsQueryable(IEnumerable) |
Converts an IEnumerable to an IQueryable. |
Ancestors<T>(IEnumerable<T>) |
Returns a collection of elements that contains the ancestors of every node in the source collection. |
Ancestors<T>(IEnumerable<T>, XName) |
Returns a filtered collection of elements that contains the ancestors of every node in the source collection. Only elements that have a matching XName are included in the collection. |
DescendantNodes<T>(IEnumerable<T>) |
Returns a collection of the descendant nodes of every document and element in the source collection. |
Descendants<T>(IEnumerable<T>) |
Returns a collection of elements that contains the descendant elements of every element and document in the source collection. |
Descendants<T>(IEnumerable<T>, XName) |
Returns a filtered collection of elements that contains the descendant elements of every element and document in the source collection. Only elements that have a matching XName are included in the collection. |
Elements<T>(IEnumerable<T>) |
Returns a collection of the child elements of every element and document in the source collection. |
Elements<T>(IEnumerable<T>, XName) |
Returns a filtered collection of the child elements of every element and document in the source collection. Only elements that have a matching XName are included in the collection. |
InDocumentOrder<T>(IEnumerable<T>) |
Returns a collection of nodes that contains all nodes in the source collection, sorted in document order. |
Nodes<T>(IEnumerable<T>) |
Returns a collection of the child nodes of every document and element in the source collection. |
Remove<T>(IEnumerable<T>) |
Removes every node in the source collection from its parent node. |