EntityCollection<TEntity>.Add(TEntity) Metodo

Definizione

Aggiunge un oggetto all'insieme.

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

Oggetto da aggiungere all'insieme. entity deve implementare IEntityWithRelationships.

Implementazioni

Eccezioni

entity è null.

Esempio

Questo esempio è basato sul modello Adventure Works Sales. Per eseguire il codice in questo esempio, è necessario aver già aggiunto il modello AdventureWorks Sales al progetto e configurato il progetto per l'uso di Entity Framework. A tale scopo, completare le procedure in Come: Configurare manualmente entity Framework Project e Come: Definire manualmente i file di modello e mapping.

Questo esempio crea due nuove SalesOrderHeader entità, le aggiunge all'entità Contact e, dopo aver rimosso un oggetto, usa il Add metodo per aggiungere nuovamente l'oggetto alla raccolta.

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)
            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);
    }
}

Commenti

Il Add metodo aggiunge un oggetto a un EntityCollection<TEntity> oggetto e crea una relazione tra i due oggetti . Quando l'oggetto di origine è associato a un'istanza ObjectContext di , il Add metodo aggiunge anche l'oggetto all'oggetto ObjectContext. Questa operazione viene convertita in un'operazione di inserimento nell'origine dati quando SaveChanges viene chiamata. Per altre informazioni, vedere Creazione, aggiunta, modifica ed eliminazione di oggetti.

Il Add metodo può essere chiamato più volte nella stessa istanza dell'oggetto.

Si applica a