C# Efficiently delete 50000 records in batches using SQLBulkCopy or equivalent library

后端 未结 3 1034
一个人的身影
一个人的身影 2020-12-19 09:30

I\'m using this library to perform bulk delete in batches like following:

  while (castedEndedItems.Any())
  {
    var subList = castedEndedItems.Take(4000).         


        
3条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-19 09:57

    If you don't mind the extra dependency, you could use the NuGet package Z.EntityFramework.Plus.

    The code is roughly as follows:

    using Z.EntityFramework.Plus;
    [...]
             using (yourDbContext context = new yourDbContext())
             {
                  yourDbContext.yourDbSet.Where( yourWhereExpression ).Delete();
             }
    

    It is simple and efficient. The documentation contains exact numbers about the performance.

    Regarding licensing: As far as I know, version 1.8 has an MIT license: https://github.com/zzzprojects/EntityFramework-Plus/blob/master/LICENSE The newer version are not free to use.

提交回复
热议问题