Entity Framework Core Plus Batch Delete
Deleting using Entity Framework Core 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 Core & Client Evaluation
Do not use this feature if Client Evaluation is enabled
Batch Delete use the SQL generated by EF Core. When a filter is made on client-side, it means the filtering happens in the application and not in the SQL executed.
In other words, even if you put a filter, all rows tables could be potentially deleted if the filter is made on the client-side.
We always recommend to disable the client evaluation to avoid performance issue in your application.
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.