We have a huge performance problem in our app, we collect data from different systems and save it in our DB to later generate some reports.
We use Entity framework 4.2 and DbSets, is there a better way of doing bulk inserts then the Add method for each entity on the DbSet? Because there are huge performance problems with the Add logic
oneThousandCustomers.Foreach(c => context.Customers.Add(c)); //This will take a minute
context.SaveChanges(); //This takes under a second
I guess the add method is doing lots lookups etc and stuff under the hood, but I just want to bulk insert this data. Is it possible?
Ok so the deal here is that EF does a whole lot of stuff under the scenes when you add an item to a collection.
The really big thing in terms of performance is that it calls DetectChanges() automatically on each add command which enumerates the entire object graph.
What this means is that if you have a large amount of items which are being tracked by the current EF context this will cause some really serious performance issues.
Heres some tips to improve performance, as a comparison i can insert 1000 fairly simple entities with no FK’s to EF in under 1/2 a second.
this will ensure that your change graph is small so add will be
fairly quick.
Code to turn off auto-detect changes
When disabling AutoDetectChanges you need to be a little careful as it means that most of the EF automagic is turned off so you may get some weirdness especially if you are using navigation properties or are updating existing entities. To solve these issues call DetectChanges just before you call SaveChanges. This will decrease performance and isn’t needed with simple add operations so in your example you can probably get away without this.
This MSDN article talks about this, (note even though its EF 5 its the same in 4) http://msdn.microsoft.com/en-us/library/gg696177%28v=vs.103%29.aspx