I've seen a number of "Bulk Insert in EF" questions however all of these deal with a usecase where a user is trying to insert a large array of items.
I've a situation where I have a new Parent entity with ~1500 new related entities attached to it. Both the parent and the child entities are mapped to their own tables in EF.
At the moment I'm using something like:
//p is already created and contains all the new child items
public void SaveBigThing(Parent p){
if(p.Id == 0){
// we've got a new object to add
db.BigObjects.Add(p);
}
db.SaveChanges();
}
Entity Framework at the moment creates an individual insert statement for each and every child item. Which takes 50 seconds or so. I want to be able to use db.ChildEntity.AddRange(items)
But I'm unsure if there's a better way than to use 2 separate operations. First create the parent to get it's Id then a AddRange
for all the child items?