This post was most recently updated on January 28th, 2024.
3 min read.This article offers yet another possible fix to an issue, where trying to call SaveChanges() in Entity Framework Core throws a pretty generic “An error occurred while updating the entries”-exception, and you’re left wondering what in tarnation is wrong this time.
And admittedly, that’s a really generic error, so it could pretty much be whatever. But in this article, I’ll go through one possibility – hopefully, it helps!
Problem
So I was just pushing in some new rows to an incredibly simple table in my small Azure MS SQL database when this error occurred:
An error occurred while updating the entries. See the inner exception for details.
This error can be thrown due to incredibly many different reasons. And there are different variants of the error – some of them below:
microsoft.entityframeworkcore.dbupdateexception: 'an error occurred while updating the entries. see the inner exception for details.
microsoft.entityframeworkcore.dbupdateexception: 'an error occurred while saving the entity changes. see the inner exception for details.'
What gives? 🤔
Reason
Huh, so the actual error message itself is extremely generic. That’s not going to be enough to help us figure this out.
But what about the details of the exception – you can catch it and, like the error proposes, take a look at the inner exception and associated HResult – it’s bound to contain an error code, right?
Well, yes. The HResults were:
- Exception: -2146233088 (Generic, doesn’t help us much)
- InnerException: -2146232060 (Generic SQL Server error)
Ugh – that’s extremely generic as well! No help at all.
But wait – let’s do what it tells us to, and see the inner exception, then:
{"Cannot insert explicit value for identity column in table '[Not the table I was inserting stuff to, and not one that had any direct relations to it either]' when IDENTITY_INSERT is set to OFF.\r\nCannot insert explicit value for identity column in table '[Another unrelated table]' when IDENTITY_INSERT is set to OFF.\r\nCannot insert explicit value for identity column in table '[One more unrelated table]' when IDENTITY_INSERT is set to OFF.\r\nCannot insert explicit value for identity column in table '[Yet another unrelated table]' when IDENTITY_INSERT is set to OFF.\r\nCannot insert explicit value for identity column in table '[This one was just as unrelated]' when IDENTITY_INSERT is set to OFF."}
That’s, uhh… Not that helpful, still? What’s up with all of these errors from other tables??
Oh. Wait. The exception only contains references to other tables, not about my actual entity at all? And that’s going to be pretty important.
See – those references are going to be the key term here.
Solution
Okay – this is going to be specific to this particular case, and probably different for you, but might be worth checking out anyway.
So, I was handling some non-tracked entities earlier in the code – and was in fact trying to associate one of these entities with a new entity that I was saving.
In the code, this looks somewhat like the below:
var item = ctx.Entities.Where(x => x.Amount > 1000).AsNoTracking().First();
ctx.OtherEntities.Add(new OtherEntity(){
Id = 0,
Entity = item
});
ctx.SaveChanges();
Did you catch it? The item is not tracked – and you can’t associate it with a tracked entity!
So, what you need to do, is to fetch the item without.AsNoTracking() if you plan on associating it with any tracked entities.
Super simple – but the exception thrown is definitely not very informative.
In case this article didn’t help you, your underlying cause might be different. But luckily, I have another one explaining another solution to the same error (but a different underlying issue) here:
- “Performing cleanup” – Excel is stuck with an old, conflicted file and will never recover. - November 12, 2024
- How to add multiple app URIs for your Entra app registration? - November 5, 2024
- How to access Environment Secrets with GitHub Actions? - October 29, 2024