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!
The INSERT statement in MySQL also supports the use of VALUES syntax to insert multiple rows as a bulk insert statement. To do this, include multiple lists of column values, each enclosed within parentheses and separated by commas.
The EF Bulk Insert feature let you insert thousands of entities in your database efficiently. This feature is provided by the library EF Extensions (Included with EF Classic). EF Extensions is used by over 2000 customers all over the world and supports all Entity Framework versions (EF4, EF5, EF6, EF Core, EF Classic).
You can add multiple records or multiple objects using the AddRange method of DbSet as shown in the following code. The code creates a list of department objects and inserts two new departments to the list. We add the list to the context using the AddRange method.
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 currentDbContext
as a parameter and require to return a newDbContext
. The currentDbContext
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With