Why is Entity Framework so slow to add multiple items in one SaveChanges()?

c# entity-framework entity-framework-6

Accepted Answer

I found this informative article while doing research for your query: http://www.codinghelmet.com/?path=howto/bulk-insert

I will quote:

Each object that was inserted required two SQL statements - one to insert a record, and additional one to obtain identity of the new record

As soon as many records are inserted, this becomes an issue. One record is added at a time, which exacerbates the issue (although this is unrelated to your concern as you are already trying the one-by-one insert). Thus, if you are entering 200 records, 400 sql statements will be run one at a time.

So far as I can tell, EF is simply not designed for bulk insertion. even if it's only 200 records being inserted. Which strikes me as a major letdown.

I began to consider, "What then is EF useful for? It is incapable of even inserting a few records ". I will praise EF in two areas:

  1. Selecting a query Writing queries and swiftly importing data into your application are both extremely simple processes.
  2. easing the input of complicated records. If you've ever attempted to insert all the linked entries into a table with a lot of foreign keys in a single transaction, you know what I'm talking about. Thankfully, EF connects all the linked data in a single transaction and inserts each record sequentially. But as was already said, there is a price for this.

Simply said, it seems that using SqlBulkCopy may be the best option if your procedure involves inserting a large number of entries. This can quickly insert thousands of entries.

Since I use EF often, I realise that this may not be the answer you want to hear, but I don't see any other options.

5
8/1/2017 2:06:39 AM

Popular Answer

ZZZ_tmp


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow