EntityCollection<TEntity>.Add(TEntity) 方法

定義

將物件加入至集合。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)

參數

entity
TEntity

要加入集合的物件。An object to add to the collection. entity 必須實作 IEntityWithRelationshipsentity must implement IEntityWithRelationships.

實作

例外狀況

entitynullentity is null.

範例

這個範例是以「 艾德作品」銷售模型為基礎。This example is based on the Adventure Works Sales Model. 若要執行此範例中的程式碼,您必須已經將 AdventureWorks Sales Model 加入到專案中,並設定您的專案使用 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. 若要這樣做,請完成 how to:手動設定 Entity Framework 專案 和 how To:手動定義模型和對應檔中的程式。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.

這個範例會建立兩個新的 SalesOrderHeader 實體、將它們加入至 Contact 實體,然後在移除物件之後,使用 Add 方法,將此物件重新加入至集合。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

備註

Add 方法會將物件加入至 EntityCollection<TEntity> 並且建立這兩個物件之間的關聯性。The Add method adds an object to an EntityCollection<TEntity> and creates a relationship between the two objects. 當來源物件附加至 ObjectContext 執行個體時,Add 方法也會將此物件加入至 ObjectContextWhen the source object is attached to an ObjectContext instance, the Add method also adds the object to the ObjectContext. 呼叫 SaveChanges 時,這項作業就會在資料來源中轉譯成插入作業。This operation is translated into an insert operation in the data source when SaveChanges is called. 如需詳細資訊,請參閱 建立、加入、修改和刪除物件For more information, see Creating, Adding, Modifying, and Deleting Objects.

Add 方法可以在同一個物件執行個體上呼叫多次。The Add method can be called multiple times on the same object instance.

適用於