Entity Framework Core generating -ve integer values for Primary Key column on a table row add

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


I am using EF Core v2.2 for inserting and updating rows into a table.

While I am doing that, I am looping based on a list and trying to insert into the table. For the 1st row insert the code works as expected.

However from the second item in the list the row doesn't get inserted into the table.

When I debugged, I found out that after the _rmsContext.save() the 2nd inserts the primary key value for the table is generated as -9223372036854774802.

Here is my code

foreach (string account in accntList)
     var crossBowAccounts = _cbContext.SomeTableRepository
                                      .FindBy(ca => ca.AccountId == account && 
                                                    ca.ClientId == model.ClientId)

     AccountHistory accountHistory = new AccountHistory()
                                AccountId = account,
                                ClientId = model.ClientId,
                                UserId = userId,
                                ActionName = crossBowAccounts == null ? "Add" : "Edit"


     // Some more insert/update code for a table in a different context .

Here model is being passed as an input to the method.

This is what I observed during debugging:

enter image description here

This is my table schema

enter image description here

2/26/2020 5:53:36 AM

Popular Answer

Looks like you are hitting this issue here: https://github.com/dotnet/efcore/issues/6147

EF Core generates temporary IDs which are then replaced with the real IDs when SaveChanges is called

It's not exactly a bug and it has been changed again in 3.1.

So until the item is inserted, a negative and/or random number makes sense. You need to see why it's not inserted (the negative id is not the reason).

2/26/2020 5:11:58 AM

Related Questions


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