Bulk-Insert to MySQL in Entity-Framework Core

Viewed 5485

I have a list consisting of ~10,000 objects (let's say of class Person) that I need to insert to a MySQL table. If I use the regular DbContext.SaveChanges(), it takes 60-70 seconds to issue, which I need to reduce drastically. I've found several extensions for bulk-insertions:

Unfortunately, non seem to exist for MySQL databases. Does anybody know of one for MySQL? If not, could anyone give me an approach as to how I could make my own or adjust the aforementioned solutions? Thank you!

2 Answers

Z.EntityFramework.Extensions.EFCore is a paid solution that might help solve your problem.

It is has support for both .NET and .NET Core!

I stumbled upon the same situation as the person who asks this question, but the answers above are very unclear. Here's how you can implement bulk-insert in a .NET Core 3.1 application:

1.I added the following nuget package into my application: Z.EntityFramework.Extensions.EFCore

2.I added a parameterless constructor in my DataContext class like this:

public YourDataContextClass()
{
}

3.In your business logic where you have a list of objects and want to bulk insert:

EntityFrameworkManager.ContextFactory = _context => new YourDataContextClass();
_context.BulkInsert(lstObjects);
// no need to call SaveChanges() / SaveChangesAsync(). BulkInsert() / BulkInsertAsync() 
// saves into DB itself

If you want to use async version then:

  await _context.BulkInsertAsync(lstObjects, cancellationToken);

For more info on above points: https://entityframework-extensions.net/bulk-insert

What this line EntityFrameworkManager.ContextFactory = _context => new YourDataContextClass(); do?

As per their documentation:

The context factory is a function Func<DbContext, DbContext> that provides the current DbContext as a parameter and require to return a new DbContext. The current DbContext is passed in a parameter in case you need to create a working context that depends on the current context configuration or type.

For more information about it, follow this link:

https://entityframework-extensions.net/context-factory

Have you trying disabling traking inside the dbContext you may write:

ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.NoTracking;
Related