Fastest Way of Inserting in Entity Framework

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

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

    There are some third-party libraries supporting Bulk Insert available:

    • Z.EntityFramework.Extensions (Recommended)
    • EFUtilities
    • EntityFramework.BulkInsert

    See: Entity Framework Bulk Insert library

    Be careful, when choosing a bulk insert library. Only Entity Framework Extensions supports all kind of associations and inheritances and it's the only one still supported.


    Disclaimer: I'm the owner of Entity Framework Extensions

    This library allows you to perform all bulk operations you need for your scenarios:

    • Bulk SaveChanges
    • Bulk Insert
    • Bulk Delete
    • Bulk Update
    • Bulk Merge

    Example

    // Easy to use
    context.BulkSaveChanges();
    
    // Easy to customize
    context.BulkSaveChanges(bulk => bulk.BatchSize = 100);
    
    // Perform Bulk Operations
    context.BulkDelete(customers);
    context.BulkInsert(customers);
    context.BulkUpdate(customers);
    
    // Customize Primary Key
    context.BulkMerge(customers, operation => {
       operation.ColumnPrimaryKeyExpression = 
            customer => customer.Code;
    });
    

提交回复
热议问题