Fastest Way of Inserting in Entity Framework

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

    I would recommend this article on how to do bulk inserts using EF.

    Entity Framework and slow bulk INSERTs

    He explores these areas and compares perfomance:

    1. Default EF (57 minutes to complete adding 30,000 records)
    2. Replacing with ADO.NET Code (25 seconds for those same 30,000)
    3. Context Bloat- Keep the active Context Graph small by using a new context for each Unit of Work (same 30,000 inserts take 33 seconds)
    4. Large Lists - Turn off AutoDetectChangesEnabled (brings the time down to about 20 seconds)
    5. Batching (down to 16 seconds)
    6. DbTable.AddRange() - (performance is in the 12 range)

提交回复
热议问题