EF Core provides the Remove
and RemoveRange
methods for deleting entities. However, it may not be the best option for deleting large amounts of data from a database.
The fastest and easiest way of deleting multiple entities is by using the Entity Framework Extensions third-party library.
This library is very fast and offers hundreds of features and all other bulk operations to support the most common cases and many complex ones.
Bulk Extensions Delete
Entity Framework Extensions provides the BulkDelete
extension method can be used to efficiently delete large amounts of data from a database.
By default, you only need to pass your entities in the parameter, and the library will find the primary key from your database.
using (var context = new MyDbContext())
{
context.BulkDelete(customerToDeletes);
}
In this example, the BulkDelete
method is used to delete all customers from customerToDeletes
. The BulkDelete
method is much faster than deleting entities one by one, so it's ideal for large-scale deletions.
Note
- NuGet Package: https://www.nuget.org/packages/Z.EntityFramework.Extensions.EFCore
- Documentation: Entity Framework Extensions – Bulk Delete