|
Este artículo proviene de un motor de traducción automática. Mueva el puntero sobre las frases del artículo para ver el texto original. Más información.
|
Traducción
Original
|
EntityCollection<TEntity>.Add (Método)
Espacio de nombres: System.Data.Objects.DataClasses
Ensamblado: System.Data.Entity (en System.Data.Entity.dll)
Parámetros
- entity
- Tipo: TEntity
Un objeto que se va a agregar a la colección. entity debe implementar IEntityWithRelationships.
Implementaciones
ICollection<T>.Add(T)| Excepción | Condición |
|---|---|
| ArgumentNullException |
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); } }
Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (no se admite el rol Server Core), Windows Server 2008 R2 (se admite el rol Server Core con SP1 o versiones posteriores; no se admite Itanium)
.NET Framework no admite todas las versiones de todas las plataformas. Para obtener una lista de las versiones compatibles, vea Requisitos de sistema de .NET Framework.