开发者

entity framework inserting multiple objects

开发者 https://www.devze.com 2023-02-03 21:48 出处:网络
I am trying to insert multiple objects into my db. foreach (empl开发者_C百科oyee employeedata in employeelist)

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
}
0

精彩评论

暂无评论...
验证码 换一张
取 消