Skip to content

Instantly share code, notes, and snippets.

@kristofclaes
Created July 27, 2011 10:43
Show Gist options
  • Save kristofclaes/1109126 to your computer and use it in GitHub Desktop.
Save kristofclaes/1109126 to your computer and use it in GitHub Desktop.
I don't like this, but I don't want to add too much business logic to a stored procedure either. How can this be improved?
var context = new DbContext();
foreach(var itemId in itemIds) // on average about 10 items
{
var item = new Item();
item.ForeignKey = itemId;
item.SomeProperty = someValue;
foreach(var subItemId in subItemsIds) // on average about 20 subitems
{
var subItem = new SubItem();
subItem.ForeignKey = subItemId;
subItem.SomeProperty = someValue;
foreach(var date in dates) // on average about 30 dates
{
var subSubItem = new SubSubItem();
subSubItem.Date = date;
subSubItem.SomePropery = someValue;
subItem.SubSubItems.Add(subSubItem);
}
item.SubItems.Add(subItem);
}
context.Items.Add(item);
}
context.SaveChanges(); // about 6.100 inserts
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment