Supercharge Your EF Core Performance with BulkInsertAsync

Having issues with large data saving/insert in your .NET applications? EF Core’s BulkInsertAsync could be the solution you need. Simply install EFCore.BulkExtensions from NuGet. Here’s a quick guide to help you get started

🚀 Supercharge Your EF Core Performance with BulkInsertAsync! 🚀

Struggling with large data insertions in your .NET applications? EF Core’s BulkInsertAsync can be a game-changer. Just install EFCore.BulkExtensions from nuget. Here’s a quick guide to help you get started:

Why Use BulkInsertAsync?

1. Efficiency: Inserts multiple records in a single database round trip.
2. Performance: Significantly reduces the time taken for bulk operations.
3. Simplicity: Easy to implement with minimal code changes.

Example
Let’s say we have a Student entity and we want to insert a large list of students into the database.

using EFCore.BulkExtensions;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Threading.Tasks;
public class Student
{
public int StudentId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Branch { get; set; }
}
public class ApplicationDbContext : DbContext
{
public DbSet<Student> Students { get; set; }
}
public async Task BulkInsertStudentsAsync(List<Student> students)
{
using var context = new ApplicationDbContext();
await context.BulkInsertAsync(students);
}
using EFCore.BulkExtensions; using Microsoft.EntityFrameworkCore; using System.Collections.Generic; using System.Threading.Tasks; public class Student { public int StudentId { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public string Branch { get; set; } } public class ApplicationDbContext : DbContext { public DbSet<Student> Students { get; set; } } public async Task BulkInsertStudentsAsync(List<Student> students) { using var context = new ApplicationDbContext(); await context.BulkInsertAsync(students); }
using EFCore.BulkExtensions;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Threading.Tasks;
public class Student
{
    public int StudentId { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Branch { get; set; }
}
public class ApplicationDbContext : DbContext
{
    public DbSet<Student> Students { get; set; }
}
public async Task BulkInsertStudentsAsync(List<Student> students)
{
    using var context = new ApplicationDbContext();
    await context.BulkInsertAsync(students);
}

EF Core SaveChangesAsync:

  • 1,000 records: 18 ms
  • 10,000 records: 203 ms
  • 100,000 records: 2,129 ms

EF Core BulkInsertAsync:

  • 1,000 records: 8 ms
  • 10,000 records: 76 ms
  • 100,000 records: 742 ms1

With BulkInsertAsync, you can handle large data operations efficiently and keep your application running smoothly. Give it a try and see the difference!


Notice Inappropriate?

If you come across any inappropriate content, please report it to our administrators!

Leave a Reply

Please login to post comments.

By clicking ”Accept”, you agree to the storing of cookies on your device to enhance site navigation, analyze site usage, and improve marketing.
Top