Is it possible to insert multiple child records using the entity framework?
Using entity framework. Is it possible to insert multiple child records. At the moment I am iterating through a list and adding to entity object, but only one/the last object is saved to the database. Should I just create a sproc, or is this possible with EF? e.g.
//dc = datacontext
var fileList = Session["FileNames"];
string[] sA = fileList.ToString().Split('|');
for (int i = 0; i < sA.Count(); i++)
{
wcc.ID = id; //which has been supplied through a previous
//dc.SaveChanges
wcc.FileName = sA[i];
dc.AddTotbObject(wcc);
}
dc.SaveChanges();
dc.Dispose(开发者_StackOverflow中文版);
You're only ever adding a single instance. You'll need to do something along these lines within your loop:
wcc = new tbObject();
wcc.ID = id;
wcc.FileName = sA[i];
dc.AddTotbObject(wcc);
精彩评论