7

I was trying to Add() about 18000 objects to my DBContext. It took around 5 minutes. Saving this data with SaveChanges() took even longer. I switched to creating a normal List and adding my objects to it, whereafter i used SqlBulkCopy to persist the data. This took about 5 seconds.

What does the Add method do, that makes it take so long?

Kenci
  • 4,435
  • 12
  • 60
  • 98
  • 1
    (+1). Also are you talking about something like this?: http://stackoverflow.com/questions/5940225/fastest-way-of-inserting-in-entity-framework – AliRıza Adıyahşi Dec 07 '12 at 14:54

1 Answers1

2

So what happens is that on each add call DetectChanges is executed on the context. This enumerates the entire object graph, so the more items you track the longer each individual add will take. Theres a bunch of tuning you can do around this as well to stuff go fast (i get approx 1k/s inserts on my home vm).

Effectively without tuning EF add performance is O(n^2)

I go into this in quite a bit of detail in the following article:

EntityFramework Performance and AutoDetectChanges

For more deets about how fast EF can perform when tuned take a look here:

Entity Framework Comparative Performance

Not loved
  • 30,848
  • 21
  • 111
  • 180