Entity Framework Efficiently insert data method
When using transactionscope for transaction processing, how to quickly insert more than 4,000 records into the database?
Answer:
For a large amount of data insertion (4000 records), the method efficiency is extremely low. The following optimization technology can significantly improve the insertion speed:
SaveChanges()
One -time
SaveChanges()
Batch : SaveChanges()
After handling the specified quantity (eg, 100 items), call SaveChanges()
Reconstruction context: Similar to the batch method, but release the context and create a new context to release the additional entity after SaveChanges()
. SaveChanges()
to . SaveChanges()
Configuration.AutoDetectChangesEnabled
The following code fragment combines the above optimization technology: false
. In addition, the release and re -creation of the context will help to remove the additional entities, thereby reducing memory use and improving performance. Performance comparison:
Use this optimization mode to insert 560,000 entities into the performance test results of the database:
<code class="language-csharp">using (TransactionScope scope = new TransactionScope()) { MyDbContext context = null; try { context = new MyDbContext(); context.Configuration.AutoDetectChangesEnabled = false; int count = 0; foreach (var entityToInsert in someCollectionOfEntitiesToInsert) { ++count; context = AddToContext(context, entityToInsert, count, 100, true); } context.SaveChanges(); } finally { if (context != null) context.Dispose(); } scope.Complete(); } private MyDbContext AddToContext(MyDbContext context, Entity entity, int count, int commitCount, bool recreateContext) { context.Set<Entity>().Add(entity); if (count % commitCount == 0) { context.SaveChanges(); if (recreateContext) { context.Dispose(); context = new MyDbContext(); context.Configuration.AutoDetectChangesEnabled = false; } } return context; }</code>
SaveChanges()
, SaveChanges()
: 202 seconds
, : 164 seconds
In contrast, if you call
for each record, it will take hours to complete the same operation.The above is the detailed content of What's the Fastest Way to Insert 4000 Records into a Database Using Entity Framework within a TransactionScope?. For more information, please follow other related articles on the PHP Chinese website!