Hi I'm looking for efficient way to delete multiple records at once. I'm deleting 400 records and it takes 8-15 seconds. Here is my code
using (var entities = new Entity())
{
foreach (Item item in entities.Items.Where(x => x.id == id))
entities.DeleteObject(item);
entities.SaveChanges();
}
You can do it faster using EntityFramework.Extensions
1) First install EntityFramework.Extensions using NuGet
2) Here is the code similar to Linq2Sql's DeleteAllOnSubmit():
using EntityFramework.Extensions;
....
using (var entities = new Entity())
{
entities.Items.Delete(x => x.id == id);
entities.SaveChanges();
}
...
Check out Bulk-deleting in LINQ to Entities or PLINQO for Entity Framework if it's the sort of delete you could do in a single batch, i.e.
DELETE FROM Entities WHERE [some condition]
Otherwise, maybe check you've got an index on the x
column you're using to find each record.
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