1

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,

2 Answers 2

6

Just iterate through your list again and check Ids. If you are using autogenerated column in DB, Ids will be filled.

Sign up to request clarification or add additional context in comments.

Comments

1

do some thing like this

foreach (employee employeedata in employeelist)
{
 employeeA.id // Will give you id
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.