Fastest Way of Inserting in Entity Framework

前端 未结 30 2792
鱼传尺愫
鱼传尺愫 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 06:04

    One of the fastest ways to save a list you must apply the following code

    context.Configuration.AutoDetectChangesEnabled = false;
    context.Configuration.ValidateOnSaveEnabled = false;
    

    AutoDetectChangesEnabled = false

    Add, AddRange & SaveChanges: Doesn't detect changes.

    ValidateOnSaveEnabled = false;

    Doesn't detect change tracker

    You must add nuget

    Install-Package Z.EntityFramework.Extensions
    

    Now you can use the following code

    var context = new MyContext();
    
    context.Configuration.AutoDetectChangesEnabled = false;
    context.Configuration.ValidateOnSaveEnabled = false;
    
    context.BulkInsert(list);
    context.BulkSaveChanges();
    

提交回复
热议问题