Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy does DbSet.Add work so slow?
    text
    copied!<p>The same topic was discussed here 8 months ago: <a href="https://stackoverflow.com/questions/4355474/how-do-i-speed-up-dbset-add">How do I speed up DbSet.Add()?</a>. There was no solution proposed other than using SqlBulkCopy which is not acceptable for us. I've decided to bring it up once again hoping there might be new thoughts and ideas around this issue and other workarounds are proposed. At least I'm just curious why this operation takes so long time to run.</p> <p>Well, the problem is: I have to update 30K entities into database (EF 4.1, POCO). The entity type is quite simple containing integer Id + other 4 integer properties with no relations to other types. 2 cases:</p> <ul> <li><p>all them are new records. Running context.Entities.Add(entity) one by one for every entity takes 90 seconds with Cntx.Configuration.AutoDetectChangesEnabled=false (true value makes it run forever). Then SaveChanges takes just a second. Other approach: attaching it to the context like this takes the same 90 sec:</p> <pre><code>Cntx.Entities.Attach(entity); Cntx.Entry(entity).State = EntityState.Added; </code></pre></li> <li><p>all them are existing records with some changes. In the case it takes just few milliseconds to attach it to existing data context like this:</p> <pre><code>Cntx.Entities.Attach(entity); Cntx.Entry(entity).State = EntityState.Modified; </code></pre> <p>See the difference?</p></li> </ul> <p>What is behind the scene of Add method that makes it work so incredibly slow?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload