I am trying to insert multiple objects into my db.
foreach (empl开发者_C百科oyee employeedata in employeelist)
{ objectcontext.employees.AddObject(employeedata); } objectcontext.SaveChanges();
I call objectcontext.savechanges outside the loop so that it is efficient. The problem is that I would like to get a list of primary keys that are generated by the db.
If I am inserting a single object using objectcontext.employees.AddObject(employeeA)
I could get the id after saving changes as employeeA.id
. I am not sure of how to go about this now that I am adding a list of objects to the object context and then calling savechanges that inserts these into the db. Am I missing something obvious here? Thanks,
Just iterate through your list again and check Ids. If you are using autogenerated column in DB, Ids will be filled.
do some thing like this
foreach (employee employeedata in employeelist)
{
employeeA.id // Will give you id
}
精彩评论