1

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:

enter image description here

This is my table schema

enter image description here

3
  • AccountHistoryId is primary key column with Identity configured?
    – Chetan
    Feb 26, 2020 at 4:55
  • @ChetanRanpariya Yes, i have updated my post with the screen shot of table schema.
    – Shekar.gvr
    Feb 26, 2020 at 5:12
  • The code throws any error when AccountHistoryId has negative value? Does AccountHistoryId has valid value after _rmsContext.Save(); is executed?
    – Chetan
    Feb 26, 2020 at 5:16

2 Answers 2

2

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
  • I may have not conveyed correctly, but the -ve value is generated after the _context.save() is executed. i.e. for the 2nd item in the loop.
    – Shekar.gvr
    Feb 26, 2020 at 5:55
  • I got that, but I'm pretty sure that it isn't saved. Can you share the Repository code? Maybe we can figure out why the new object is not attached to the context or why the save is not done. Feb 26, 2020 at 6:46
1

it is not bug, id is generated randomly before call SaveChanges method. If you do debug after SaveChanges, Id will be correct.

2
  • I may have not conveyed correctly before, but the -ve value is generated after the _context.save() is executed. The screen shot that i have shared is after savechanges is executed.
    – Shekar.gvr
    Feb 26, 2020 at 5:56
  • @Shekar.gvr hmm, did you check mapping ? Feb 26, 2020 at 6:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.