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)
.FirstOrDefault();
AccountHistory accountHistory = new AccountHistory()
{
AccountId = account,
ClientId = model.ClientId,
UserId = userId,
ActionName = crossBowAccounts == null ? "Add" : "Edit"
};
_rmsContext.AccountHistoryRepository.Add(accountHistory);
_rmsContext.Save();
// Some more insert/update code for a table in a different context .
_rmsContext.Save();
}
Here model
is being passed as an input to the method.
This is what I observed during debugging:
This is my table schema
AccountHistoryId
is primary key column with Identity configured?AccountHistoryId
has negative value? DoesAccountHistoryId
has valid value after_rmsContext.Save();
is executed?