Bulk load items to a EF 4.1 Code First DbContext
Jul 12
2011
I have some logic to import data (from an Excel file) into the database using EF.
var configurations = data.LoadConfigurations(configurationsExcel, componentTypes); foreach (var config in configurations) { context.Configurations.Add(config); foreach (var elem in config.Elements) { context.ConfigElements.Add(elem); foreach (var part in elem.Parts) { context.ConfigParts.Add(part); } } }
This appeared to be really slow, so I did a simple performance measurement, and it turned out like this:
However, StackOverflow to the rescue:
http://stackoverflow.com/questions/4355474/how-do-i-speed-up-dbset-add
After setting the
context.Configuration.AutoDetectChangesEnabled = false;
The whole operation went back to a linear result.
I’m not sure if the spikes are because of my way of measuring ( using the
Stopwatch class ) or if these are still related to EF.
In my case, the performance is now acceptable so, we are moving on.
Comments
No comments yet. Be the first!