I'd like to bulk delete records from a table using linq. There's a post that describes how to do it: Bulk-deleting in LINQ to Entities
var query = from c in ctx.Customers
where c.SalesPerson.Email == "..."
select c;
query.Delete();
But the function "Delete" doesn't exist in my var variable.
Furthermore, the function "SubmitChanges" doesn't exist on my context.
RemoveRange. The RemoveRange method is used for deleting multiple objects from the database in one method call. The following code deletes a large number of records from the database using RemoveRange.
Delete a Record In Connected Scenario, you can use the Remove or RemoveRange method to mark the record as Deleted . In Disconnected Scenario, you can attach it to the context and set its state as Deleted . Calling SaveChanges will send the delete query to the database.
The Soft Delete feature allows you to flag entities as deleted (Soft Delete) instead of deleting them physically (Hard Delete). The soft delete feature can be achieved by using the 'IEFSoftDelete' interface. By default, this interface is always added to the manager.
The BulkDelete method is fast but also flexible to let you handle various scenarios in Entity Framework such as: What is supported? All Entity Framework versions (EF4, EF5, EF6, EF Core, EF Classic) And more! The BulkDelete and BulkDeleteAync methods extend your DbContext to let you delete a large number of entities in your database.
The EF BulkDelete extension method let you delete a large number of entities in your database. A lot of factors might affect the benchmark time such as index, column type, latency, throttling, etc. The BulkDelete method is fast but also flexible to let you handle various scenarios in Entity Framework such as:
While being very efficient, Entity Framework (EF) & Entity Framework Core (EF Core) do not allow you to natively perform bulk operations, Hopefully, there is an open-source library that solves the problem It is known as EF Core Bulk extensions. This article shows how to use EF Core Bulk Extensions on a .Net project that relies on EF Core.
Entity Framework 6 introduced DbSet.AddRange () method to add a collection of entities in one go. What basically the AddRange () method does is, it attaches a collection of entities to the context object with Added state and when we call the SaveChanges method, it will execute the INSERT SQL Command in the database for all the entities.
There is an interesting NuGet package that lets you do batch deletes and updates:
There is no currently supported bulk delete baked into Entity Framework. Its actually one of the features being discussed on codeplex now EF is open-source.
EntityFramework.Extended
provides batch delete support (you can find this in nuget) however my experience is that it has some performance issues.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With