Entity Framework Plus Batch Delete
Deleting using Entity Framework can be very slow if you need to delete hundreds or thousands of entities. Entities are first loaded in the context before being deleted which is very bad for the performance and then, they are deleted one by one which makes the delete operation even worse.
EF+ Batch Delete deletes multiple rows in a single database roundtrip and without loading entities in the context.
// using Z.EntityFramework.Plus; // Don't forget to include this. // DELETE all users inactive for 2 years var date = DateTime.Now.AddYears(-2); ctx.Users.Where(x => x.LastLoginDate < date) .Delete(); // DELETE using a BatchSize var date = DateTime.Now.AddYears(-2); ctx.Users.Where(x => x.LastLoginDate < date) .Delete(x => x.BatchSize = 1000);
- DO NOT support Complex Type
- DO NOT support TPC
- DO NOT support TPH
- DO NOT support TPT
If you need to use one of this feature, you need to use the library Entity Framework Extensions
EF+ Batch Delete is the most efficient way to delete records. You drastically improve your application performance by removing the need to retrieve and load entities in your context and by performing a single database roundtrip instead of one for every record.
Need help getting started? firstname.lastname@example.org
We welcome all comments, ideas and suggestions to improve our library.