Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bulk delete in entity framework

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.

like image 803
Edi Avatar asked Jul 21 '12 12:07

Edi


People also ask

How do I delete multiple rows in Entity Framework?

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.

How do I delete a record in Entity Framework?

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.

How do I Soft delete in Entity Framework?

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.

What is the bulkdelete method in Entity Framework?

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.

How to delete large number of entities in Entity Framework?

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:

Is it possible to perform bulk operations in Entity Framework Core?

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.

How to add a collection of entities in Entity Framework 6?

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.


2 Answers

There is an interesting NuGet package that lets you do batch deletes and updates:

like image 126
Alexey Raga Avatar answered Oct 15 '22 04:10

Alexey Raga


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.

like image 21
Not loved Avatar answered Oct 15 '22 05:10

Not loved