C# , Entity Framework bulk delete Huge performance Issue

Indudhar Gowda 426 Reputation points
2022-04-20T11:12:20.243+00:00

C# , Entity Framework bulk delete Huge performance Issue.

var db = new MyDbContext();
var itemsToDelete = db.MyTable.Where(x=>!x.active);
db.MyTable.RemoveRange(itemsToDelete);
db.SaveChanges();

I have 700000 rows to be deleted. Kindly let me know how do I solve this Problem...

More time taken in
db.MyTable.RemoveRange(itemsToDelete);
db.SaveChanges();

In the above 2 line its taking more time....Kindly let me know the best and Fastest way to Bulk Delete.

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,396 questions
SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
12,758 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,277 questions
Transact-SQL
Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
4,554 questions
{count} votes

2 answers

Sort by: Most helpful
  1. AgaveJoe 26,136 Reputation points
    2022-04-20T11:16:58.557+00:00

    Write a stored procedure to do the delete and call the stored procedure from Entity Framework.


  2. Karen Payne MVP 35,191 Reputation points
    2022-04-20T15:41:00.72+00:00

    If the constraint is it must be done with EF consider Bulk Delete library (free edition). Otherwise see the following.

    0 comments No comments