Fastest Way of Inserting in Entity Framework

前端 未结 30 2200
鱼传尺愫
鱼传尺愫 2020-11-21 05:23

I\'m looking for the fastest way of inserting into Entity Framework.

I\'m asking this because of the scenario where you have an active TransactionScope a

30条回答
  •  醉梦人生
    2020-11-21 05:37

    [2019 Update] EF Core 3.1

    Following what have been said above, disabling AutoDetectChangesEnabled in EF Core worked perfectly: the insertion time was divided by 100 (from many minutes to a few seconds, 10k records with cross tables relationships)

    The updated code is :

      context.ChangeTracker.AutoDetectChangesEnabled = false;
                foreach (IRecord record in records) {
                   //Add records to your database        
                }
                context.ChangeTracker.DetectChanges();
                context.SaveChanges();
                context.ChangeTracker.AutoDetectChangesEnabled = true; //do not forget to re-enable
    

提交回复
热议问题