DbSet<TEntity>.Remove(TEntity) Method


Begins tracking the given entity in the Deleted state such that it will be removed from the database when SaveChanges() is called.

public virtual Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntry<TEntity> Remove (TEntity entity);
abstract member Remove : 'Entity -> Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntry<'Entity (requires 'Entity : null)>
override this.Remove : 'Entity -> Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntry<'Entity (requires 'Entity : null)>
Public Overridable Function Remove (entity As TEntity) As EntityEntry(Of TEntity)



The entity to remove.



The EntityEntry<TEntity> for the entity. The entry provides access to change tracking information and operations for the entity.


If the entity is already tracked in the Added state then the context will stop tracking the entity (rather than marking it as Deleted) since the entity was previously added to the context and does not exist in the database.

Any other reachable entities that are not already being tracked will be tracked in the same way that they would be if Attach(TEntity) was called before calling this method. This allows any cascading actions to be applied when SaveChanges() is called.

Use State to set the state of only a single entity.

See EF Core change tracking for more information.

Applies to