0

In my MVC 5 application with EF 6.0, I want to insert 200+ records of Product into database using Entity Framework in one hit. [1]One way is to convert list of records into XML and pass to Stored procedure. [2] If I use DbContext.Add() , this will fire for every record ( create insert script internally). [3]If I traverse list of product and pass each record to Stored Procedure, I think this is also not good.

What is the best way to do this?

M.Sharma
  • 269
  • 6
  • 18
  • 1
    You can use BulkInsert - there are a few extensions for EF that do that. This is the first I found using a Google Search - [https://github.com/zzzprojects/EntityFramework-Extensions](https://github.com/zzzprojects/EntityFramework-Extensions) – Teodor Kurtev Apr 26 '17 at 12:39

3 Answers3

0

Disclaimer: I'm the owner of the project Entity Framework Extensions

(This library is NOT free)

This library can make your code more efficient by allowing you to save multiples entities at once. All bulk operations are supported:

  • BulkSaveChanges
  • BulkInsert
  • BulkUpdate
  • BulkDelete
  • BulkMerge
  • BulkSynchronize

Example:

// Easy to use
context.BulkSaveChanges();

// Easy to customize
context.BulkSaveChanges(bulk => bulk.BatchSize = 100);

// Perform Bulk Operations
context.BulkDelete(customers);
context.BulkInsert(customers);
context.BulkUpdate(customers);

// Customize Primary Key
context.BulkMerge(customers, operation => {
   operation.ColumnPrimaryKeyExpression = 
        customer => customer.Code;
});
Jonathan Magnan
  • 10,874
  • 2
  • 38
  • 60
0

I have several productive Web applications that use EntityFramework.BulkInsert library with this NuGet package. It works fine, but it is no longer maintained, so a better solution is to use the updated version EntityFramework.BulkInsert-ef6-ext.

It is very simple to use:

context.BulkInsert(yourListOfModels);

and it generates BULK INSERT Sql commands that run much faster that classical Enrity Framework individual INSERTs.

Alexei - check Codidact
  • 22,016
  • 16
  • 145
  • 164
0

I'm not sure if this question How to pass a table-value parameter will work for you but it is exactly what I'm using to load data into a SQL 2014 data base. Very nice and has been working for 4+ years.

Community
  • 1
  • 1
No Refunds No Returns
  • 8,092
  • 4
  • 32
  • 43