EF Insert entity with large number of new child entities

c# entity-framework entity-framework-6

Question

I've noticed a lot of "Bulk Insert in EF" inquiries, but they are all related to situations where a user is attempting to insert a sizable amount of items.

A new Parent entity that has 1500 new linked entities tied to it is my current predicament. In EF, the parent and child entities are each mapped to a different set of tables.

I currently use phrases like these:

//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();
}

Currently, Entity Framework generates a separate insert statement for each child item. which takes about 50 seconds. I desire the ability to usedb.ChildEntity.AddRange(items) But I'm not sure whether there is an alternative to using 2 different operations. Create the parent first to obtain its ID, then aAddRange for every child's item?

1
1
1/5/2016 1:06:41 PM

Popular Answer

ZZZ_tmp
0
5/23/2017 11:59:07 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow