How to use Bulk Insert in Entity Framework using O

2019-07-03 00:24发布

问题:

I would like to use Object Context instead of DbContext to call Bulk Insert in Entity Framework 6. How can I do that?

I would like to do something like

readonly ObjectContext obContext:

public void BulkInsert<T>(IEnumerable<T> items) where T : class, new()
{
    obContext.BulkInsert(items);
}

But I am not able to do this.

回答1:

With entity framework 6, you can use this nice nuget package to bulk insert, and you can use it with the DbContext object.

so something like this:

using (var db = new YourDbContext())
{
   EFBatchOperation.For(db, db.BlogPosts).InsertAll(list);
}

https://github.com/MikaelEliasson/EntityFramework.Utilities

Hope this helps.