EntityCollection<TEntity>.Add(TEntity) EntityCollection<TEntity>.Add(TEntity) EntityCollection<TEntity>.Add(TEntity) EntityCollection<TEntity>.Add(TEntity) Method

Definizione

Aggiunge un oggetto alla raccolta.Adds an object to the collection.

public:
 virtual void Add(TEntity entity);
public void Add (TEntity entity);
override this.Add : 'Entity -> unit
Public Sub Add (entity As TEntity)

Parametri

entity
TEntity TEntity TEntity TEntity

Oggetto da aggiungere all'insieme.An object to add to the collection. L'oggetto entity deve implementare IEntityWithRelationships.entity must implement IEntityWithRelationships.

Implementazioni

Eccezioni

Esempi

Questo esempio è basato il.This example is based on the . Per eseguire il codice incluso in questo esempio, è necessario avere già aggiunto il modello Sales di Adventure Works al progetto e avere configurato il progetto per l'utilizzo di Entity Framework.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. A tale scopo, completare le procedure in come: Configurare manualmente un progetto Entity Framework e come: File di Mapping e definire il modello manualmente.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.

In questo esempio vengono create due nuove entità SalesOrderHeader che vengono aggiunte all'entità Contact e, dopo avere rimosso un oggetto, viene utilizzato il metodo Add per aggiungere nuovamente l'oggetto alla raccolta.This example creates two new SalesOrderHeader entities, adds them to the Contact entity, and, after removing an object, uses the Add method to add the object back to the collection.

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

Commenti

Il metodo Add aggiunge un oggetto a EntityCollection<TEntity> e crea una relazione tra i due oggetti.The Add method adds an object to an EntityCollection<TEntity> and creates a relationship between the two objects. Quando l'oggetto di origine viene connesso a un'istanza di ObjectContext, il metodo Add aggiunge inoltre l'oggetto a ObjectContext.When the source object is attached to an ObjectContext instance, the Add method also adds the object to the ObjectContext. Questa operazione viene tradotta in un'operazione di inserimento nell'origine dati quando viene chiamato il metodo SaveChanges.This operation is translated into an insert operation in the data source when SaveChanges is called. Per altre informazioni, vedere creazione, aggiunta, modifica e l'eliminazione di oggetti.For more information, see Creating, Adding, Modifying, and Deleting Objects.

Il metodo Add può essere chiamato più volte sulla stessa istanza dell'oggetto.The Add method can be called multiple times on the same object instance.

Si applica a