Get list of Identity Ids inserted in EF Core

.net asp.net-core c# entity-framework entity-framework-core

Question

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();

}
1
0
10/9/2019 11:55:36 PM

Accepted Answer

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();

}

3
10/10/2019 1:01:03 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow