Reputation: 18521
I need to populate my database with "fake" data in order to perform some stress test on it.
Therefor, I have written some simple straightforward code to enter data.
It runs over 7 tables, creates an object for each one, and add it using :
System.Data.Entity.Add(TEntity)
As follows:
context.MyObject.Add(MyObjInstance);
And I save every few hundred objects.
Now this is way to slow, even without the commit. (I mean hours for a very few thousand objects).
Is there a faster way to add (around a few thousand) objects ?
Something like addAll()
, or anything else that will speed up the Add
method?
Thanks.
Upvotes: 1
Views: 1013
Reputation:
It may be because of repeated calls to DetectChanges()
. Some EF functions automatically call this method. See here.
If you are tracking a lot of entities in your context and you call one of these methods many times in a loop, then you may get significant performance improvements by turning off detection of changes for the duration of the loop
Set
context.Configuration.AutoDetectChangesEnabled = false;
Do your intensive .Add()
operations and set:
context.Configuration.AutoDetectChangesEnabled = true;
before calling SaveChanges()
.
Also consider using a new context for part of the bulk operations as the more entities a context tracks the worse it performs.
Upvotes: 4