How to delete multiple records in entity framework without looping using LINQ?
Asked Answered
L

2

8

I want to delete multiple records in entity framework without using a for loop or any other loop using LINQ. Something that we can do it in SQL is there any way to delete multiple records in entity framework?

Lavernlaverna answered 24/5, 2010 at 14:38 Comment(0)
C
7

What you want to do is not supported using Entity Framework. Entity Framework needs to load an object into memory, before you can delete it. This way it can do its optimistic concurrency checks.

If you really need this, you will have to do this with pure SQL or better, use a stored procedure. You can call your stored procedure with Entity Framework.

Cobaltic answered 24/5, 2010 at 16:11 Comment(0)
E
2
using (var context = new DatabaseEntities())
{
    context.ExecuteStoreCommand("DELETE FROM YOURTABLE WHERE CustomerID = {0}", customerId);
}
Eelgrass answered 7/7, 2011 at 18:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.