简体   繁体   中英

entity framework inserting multiple objects

I am trying to insert multiple objects into my db.

foreach (employee 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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM