EntityCollection<TEntity>.Remove(TEntity) 方法

定義

從集合中移除物件並將關聯性標記為待刪除。Removes an object from the collection and marks the relationship for deletion.

public:
 virtual bool Remove(TEntity entity);
public bool Remove (TEntity entity);
override this.Remove : 'Entity -> bool
Public Function Remove (entity As TEntity) As Boolean

參數

entity
TEntity

要從集合中移除的物件。The object to remove from the collection.

傳回

Boolean

如果已順利移除項目則為 true,否則為 falsetrue if item was successfully removed; otherwise, false.

實作

例外狀況

entity 物件是 nullentity object is null.

entity 物件未附加至相同的物件內容。The entity object is not attached to the same object context.

-或--or-

entity 物件沒有有效的關聯性管理員。The entity object does not have a valid relationship manager.

範例

這個範例是以「 艾德作品」銷售模型為基礎。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.

這個範例會使用 Remove 方法,從集合中移除其中一個實體,然後呼叫 Contains 方法來判斷此物件是否已經從集合中移除了。This example uses the Remove method to remove one of the entities from the collection and then calls the Contains method to determine whether the object was removed from 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

備註

Remove 方法也會刪除來源物件與從集合中移除之物件之間的關聯性。The Remove method also deletes the relationship between the source object and the object being removed from the collection. 如果此關聯性具有參考完整性 (Referential Integrity) 條件約束 (Constraint),針對相依物件呼叫 Remove 方法就會同時將此關聯性和相依物件標記為待刪除。If the relationship has a referential integrity constraint, calling the Remove method on a dependent object marks both the relationship and the dependent object for deletion. 發生這種情況的原因是,條件約束表示如果沒有父代 (Parent) 的關聯性,相依物件就無法存在。This occurs because the constraint indicates that the dependent object cannot exist without a relationship to the parent. 如需詳細資訊,請參閱 (CSDL) 的 ReferentialConstraint 元素 For more information, see ReferentialConstraint Element (CSDL).

Removefalse當指定的物件不在集合中時傳回。Remove returns false when the specified object is not in the collection.

適用於