Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bulk-Insert to MySQL in Entity-Framework Core [closed]

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:

  • EF extensions (not free, so no option)
  • BulkExtensions (no MySQL, only SQL Server)
  • EFBulkInsert (no MySQL,only SQL Server) ...

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!

like image 845
Aydo Avatar asked May 26 '17 16:05

Aydo


People also ask

Does MySQL support bulk insert?

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.

What is bulk insert in Entity Framework?

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).

How do I insert multiple rows in Entity Framework?

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.


1 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

like image 112
SU7 Avatar answered Oct 12 '22 14:10

SU7