Home > Backend Development > C++ > What's the Fastest Way to Insert 4000 Records into a Database Using Entity Framework within a TransactionScope?

What's the Fastest Way to Insert 4000 Records into a Database Using Entity Framework within a TransactionScope?

Barbara Streisand
Release: 2025-02-02 06:11:14
Original
141 people have browsed it

What's the Fastest Way to Insert 4000  Records into a Database Using Entity Framework within a TransactionScope?

Entity Framework Efficiently insert data method

Question:

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

:
    After all the records are added to the context, the disposable call
  • . SaveChanges() Batch : SaveChanges() After handling the specified quantity (eg, 100 items), call
  • once.
  • SaveChanges() Reconstruction context: Similar to the batch method, but release the context and create a new context to release the additional entity after SaveChanges().
  • Disable changes to test: temporarily disabled automatic changes to test, set SaveChanges() to . SaveChanges()
  • Implementation example: Configuration.AutoDetectChangesEnabled The following code fragment combines the above optimization technology: false
This mode achieves batch insertion by processing multiple records before calling

. 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>
Copy after login

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!

    source:php.cn
    Statement of this Website
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
    Latest Articles by Author
    Popular Tutorials
    More>
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template