Base solution for your next web application
Open Closed

bulk insert #101


User avatar
0
andonisan created

Hello,

First thank you for your great work .

I have a doubt about which is the best way to bulk insert with the repository pattern, because when i have to insert 500 rows it takes a lot of time.

I have tried disabling AutoDetectChangesEnabled but the performance still very bad

Maybe IRepository colud be extended to insertRange for this kind of insertions?

using (var unitOfWork = _unitOfWorkManager.Begin())
                  {

                      Context.Configuration.AutoDetectChangesEnabled = false;
                      foreach (var element in list)
                      {
                          Insert(element);
                      }

                      Context.Configuration.AutoDetectChangesEnabled = true;

                      unitOfWork.Complete();
                  }

5 Answer(s)
  • User Avatar
    0
    hikalkan created
    Support Team

    Hi,

    You said "I have tried disabling AutoDetectChangesEnabled but the performance still very bad". So, then it's not a solution to add such a BulkInsert method to repository. Am I wrong?

    Actually, this is a generic problem related EntityFramework itself, rather than ASP.NET Boilerplate. If you can find a performant way, you can add to your repositories and share with us. Then we can decide to add it to standard IRepository interface.

    BTW, You don't need to start a UOW in a repository method since it's automatically started for repositories.

    Thanks.

  • User Avatar
    0
    andonisan created

    Hi,

    You´re right about adding a bulk insert method only disabling autodetectchanges.

    i added to my project other open source project

    <a class="postlink" href="https://efbulkinsert.codeplex.com/">https://efbulkinsert.codeplex.com/</a>

    Now the performance is awesome for bulk inserting,

    this example is in my EnityFramework project Repositories

    public  Task SetList(List<Entity> input)
            {
               
    
                return Task.Run(() =>
                  {
                      using (var transactionScope = new TransactionScope())
                      {
    
                          Context.BulkInsert(input);
                          Context.SaveChanges();
                          transactionScope.Complete();
    
                      }
                  }
                );
            }
    

    Thanks a lot

  • User Avatar
    0
    hikalkan created
    Support Team

    That's awesome. I'll try it. Thanks a lot.

  • User Avatar
    0
    daws created

    Any plans to insert this EntityFramework.BulkInsert in abp IRepository ?

    Maybe it could be a useful feature for other users :)

    Anyway, i'll implement it myself in custom repository, since I need it today for myself :)

  • User Avatar
    0
    hikalkan created
    Support Team

    I added an issue for this request: <a class="postlink" href="https://github.com/aspnetboilerplate/aspnetboilerplate/issues/648">https://github.com/aspnetboilerplate/as ... issues/648</a> I should check this library to see if implement similar thing for ABP without dependening on the library. Thanks.