WHAT IN TARNATION EF CORE?

How to resolve another “An error occurred while updating the entries” exception in Entity Framework Core

Reading Time: 3 minutes.

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 be pretty much whatever. But in this article, I’ll go through one possibility.

Problem

Table of Contents

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.

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.

The error itself doesn’t help you much. But what about the 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 contains only references to other tables, not about my actual entity at all? References is going to be the key term here.

Solution

Okay – this is going to be specific to my 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 I was then saving.

In the code, this looks somewhat like 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, I have another one explaining another solution to the same error (but different underlying issue) here:

mm
0 0 vote
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments