EntityCollection<TEntity>::Remove Method (TEntity)
Removes an object from the collection and marks the relationship for deletion.
Assembly: System.Data.Entity (in System.Data.Entity.dll)
Parameters
- entity
-
Type:
TEntity
The object to remove from the collection.
Implements
ICollection<T>::Remove(T)| Exception | Condition |
|---|---|
| ArgumentNullException | entity object is null. |
| InvalidOperationException | The entity object is not attached to the same object context. -or- The entity object does not have a valid relationship manager. |
The Remove method also deletes the relationship between the source object and the object being removed from the collection. 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. This occurs because the constraint indicates that the dependent object cannot exist without a relationship to the parent. For more information, see ReferentialConstraint Element (CSDL).
Remove returns false when the specified object is not in the collection.
This example is based on the . 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. 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.
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.
Available since 3.5