0

Our is a healthcare application follows (AngularjS+MVC+WebAPI) 3tier architecture. Im uploading an Excel sheet n doing column mappings in an import screen. After that im doing all validations for each n every record in the excel sheet. Afterwards all these 50K rows as entities to Web-API layer. Here im inserting one rec into the parent table to get the primary key. After getting it im looping through the 50k entities to update it in the collection. Then im using addRange to update all these values into the database. Here is a piece of code we have used in the application.

            db.tableA.Add(Entity1);

            db.SaveChanges();

            (Primary Key) fileId = int.Parse(Entity1.Id.ToString());


//DataList having 50K Entities
            foreach (tableB coll in DataList)

            {

                coll.fileID = fileId;                    

            }

            db.tableB.AddRange(DataList);

            db.SaveChanges();

Referred below links. Bulk insert in entity framework

How to use Bulk Insert in Entity Framework using Object Context?

Bulk insert in entity framework

But i couldnt use the EntityFramework.BulkInsert since its not compatible with the latest EF version(6.1). Is there any other viable approach to use bulk insert using EF? Also can we use SqlBulkCopy in the Entity Framework?`

5
  • 1
    Yes you can simply use SqlBulkCopy in the same project in which you are using EF. Commented Sep 28, 2016 at 10:40
  • @Lukas yes.Is there any possibility of using bulk insert in EF version 6.1 ? Commented Sep 28, 2016 at 11:10
  • @SivaRajini why? Bulk insert has nothing to do with objects. Bulk Insert means sending a stream of records to the database as fast as possible. An ORM deals with mappings and classes, not records. It's the wrong tool for the job Commented Sep 28, 2016 at 11:22
  • 1
    @SivaRajini the links you provided end up converting the entities to DataTable or DataRow instances, then using SqlBulkCopy. You end up paying the conversion cost twice, first to create the entities, and second, to convert the entities to data tables. Just create the actual Datatables instead Commented Sep 28, 2016 at 11:26
  • @SivaRajini what is the form of your source records? DTOs, DataTable, Json string? Commented Sep 28, 2016 at 11:28

1 Answer 1

1

Here is an alternative solution by using another third party library (Compatible with Entity Framework 6.1)

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

This library is compatible with all Entity Framework Version 6.X

It does more than only Bulk Inserting by allowing to do all bulk operation your application may requires:

  • BulkSaveChanges
  • BulkInsert
  • BulkUpdate
  • BulkDelete
  • BulkMerge

Example

// Easy to use
context.BulkSaveChanges();

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

The library support all kind of inheritance and association, however, the library is not free.

Sign up to request clarification or add additional context in comments.

2 Comments

Is this a free or open source?
The library is unfortunately not free. This is the drawback for getting support and having a library that supports everything.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.