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

Definizione

Rimuove un oggetto dall'insieme e contrassegna la relazione per l'eliminazione.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

Parametri

entity
TEntity TEntity TEntity TEntity

Oggetto da rimuovere dalla raccolta.The object to remove from the collection.

Restituisce

true se l'elemento è stato rimosso correttamente; in caso contrario, false.true if item was successfully removed; otherwise, false.

Implementazioni

Eccezioni

L'oggetto entity non è collegato allo stesso contesto dell'oggetto.The entity object is not attached to the same object context.

-oppure--or- L'oggetto entity non contiene un gestore delle relazioni valido.The entity object does not have a valid relationship manager.

Esempi

Questo esempio è basato su.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 descritte in procedura: Configurare manualmente un progetto Entity Framework e procedura: Definire manualmente i filedi modello e di mapping.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 viene utilizzato il metodo Remove per rimuovere una delle entità dalla raccolta, quindi viene chiamato il metodo Contains per determinare se l'oggetto è stato rimosso dalla raccolta.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

Commenti

Il metodo Remove elimina anche la relazione tra l'oggetto di origine e l'oggetto rimosso dalla raccolta.The Remove method also deletes the relationship between the source object and the object being removed from the collection. Se la relazione ha un vincolo di integrità referenziale, la chiamata al metodo Remove su un oggetto dipendente contrassegna per eliminazione sia la relazione che l'oggetto dipendente.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. Questo si verifica perché il vincolo indica che l'oggetto dipendente non può esistere senza una relazione con il padre.This occurs because the constraint indicates that the dependent object cannot exist without a relationship to the parent. Per ulteriori informazioni, vedere elemento ReferentialConstraint (CSDL).For more information, see ReferentialConstraint Element (CSDL).

Removerestituisce false quando l'oggetto specificato non è presente nella raccolta.Remove returns false when the specified object is not in the collection.

Si applica a