Fastest Way of Inserting in Entity Framework

前端 未结 30 1962
鱼传尺愫
鱼传尺愫 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:58

    As per my knowledge there is no BulkInsert in EntityFramework to increase the performance of the huge inserts.

    In this scenario you can go with SqlBulkCopy in ADO.net to solve your problem

    0 讨论(0)
  • 2020-11-21 05:59

    The fastest way would be using bulk insert extension, which I developed

    note: this is a commercial product, not free of charge

    It uses SqlBulkCopy and custom datareader to get max performance. As a result it is over 20 times faster than using regular insert or AddRange EntityFramework.BulkInsert vs EF AddRange

    usage is extremely simple

    context.BulkInsert(hugeAmountOfEntities);
    
    0 讨论(0)
  • 2020-11-21 05:59

    But, for more than (+4000) inserts i recommend to use stored procedure. attached the time elapsed. I did inserted it 11.788 rows in 20"

    thats it code

     public void InsertDataBase(MyEntity entity)
        {
            repository.Database.ExecuteSqlCommand("sp_mystored " +
                    "@param1, @param2"
                     new SqlParameter("@param1", entity.property1),
                     new SqlParameter("@param2", entity.property2));
        }
    
    0 讨论(0)
  • 2020-11-21 06:01

    as it was never mentioned here I want to recomment EFCore.BulkExtensions here

    context.BulkInsert(entitiesList);                 context.BulkInsertAsync(entitiesList);
    context.BulkUpdate(entitiesList);                 context.BulkUpdateAsync(entitiesList);
    context.BulkDelete(entitiesList);                 context.BulkDeleteAsync(entitiesList);
    context.BulkInsertOrUpdate(entitiesList);         context.BulkInsertOrUpdateAsync(entitiesList);         // Upsert
    context.BulkInsertOrUpdateOrDelete(entitiesList); context.BulkInsertOrUpdateOrDeleteAsync(entitiesList); // Sync
    context.BulkRead(entitiesList);                   context.BulkReadAsync(entitiesList);
    
    0 讨论(0)
  • 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();
    
    0 讨论(0)
  • 2020-11-21 06:04

    Here is a performance comparison between using Entity Framework and using SqlBulkCopy class on a realistic example: How to Bulk Insert Complex Objects into SQL Server Database

    As others already emphasized, ORMs are not meant to be used in bulk operations. They offer flexibility, separation of concerns and other benefits, but bulk operations (except bulk reading) are not one of them.

    0 讨论(0)
提交回复
热议问题