0

I have a list that I am adding to a table through EF core. Table also has an identity column. I need the values of list of identity values once saveChanges is called. This function can be called my multiple progams at the same time.

using(var ctx = new DataReviewContext2())
{
   foreach(var value in values)
   {
       value.Username = user;
       value.Changed = DateTime.Now;
       ctx.ChangeLog.Add(value);
   }
   ctx.SaveChanges();

}
Learn AspNet
  • 1,192
  • 3
  • 34
  • 74
  • 2
    Have a look here: https://stackoverflow.com/questions/5212751/how-can-i-get-id-of-inserted-entity-in-entity-framework – Bill Roberts Oct 09 '19 at 23:59

1 Answers1

3

You should be able to retrieve all Ids after calling SaveChaning in that context. The id will be automatically filled for you:

using(var ctx = new DataReviewContext2())
{
   foreach(var value in values)
   {
       value.Username = user;
       value.Changed = DateTime.Now;
       ctx.ChangeLog.Add(value);
   }
   ctx.SaveChanges();
   
   // Add this to get all userIs 
   var Ids = values.Select(c=>c.UserId).ToList();

}
Moe Jallaq
  • 104
  • 5