Bulk load items to a EF 4.1 Code First DbContext

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:

image

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.

image

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.

development
Posted by: Rudi Larno
Last revised: 19 Jan, 2012 08:47 PM

Comments

No comments yet. Be the first!

blog comments powered by Disqus