EntityCollection<TEntity>.Add(TEntity) Metoda

Definicja

Dodaje obiekt do kolekcji.

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

Parametry

entity
TEntity

Obiekt, który ma zostać dodany do kolekcji. entitymusi implementować .IEntityWithRelationships

Implementuje

Wyjątki

entity to null.

Przykłady

Ten przykład jest oparty na modelu Adventure Works Sales Model. Aby uruchomić kod w tym przykładzie, musisz już dodać model AdventureWorks Sales Do projektu i skonfigurować projekt do korzystania z programu Entity Framework. Aby to zrobić, wykonaj procedury opisane w temacie Instrukcje: Ręczne konfigurowanie projektu platformy Entity Framework i Instrukcje: Ręczne definiowanie modelu i plików mapowania.

Ten przykład tworzy dwie nowe SalesOrderHeader jednostki, dodaje je do Contact jednostki, a po usunięciu obiektu używa Add metody , aby dodać obiekt z powrotem do kolekcji.

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

Uwagi

Metoda Add dodaje obiekt do obiektu EntityCollection<TEntity> i tworzy relację między dwoma obiektami. Gdy obiekt źródłowy jest dołączony do ObjectContext wystąpienia, Add metoda dodaje również obiekt do obiektu ObjectContext. Ta operacja jest tłumaczona na operację wstawiania w źródle danych, gdy SaveChanges jest wywoływana. Aby uzyskać więcej informacji, zobacz Tworzenie, dodawanie, modyfikowanie i usuwanie obiektów.

Metodę Add można wywołać wiele razy w tym samym wystąpieniu obiektu.

Dotyczy