✅ Urgent | Cannot update property in EF because another instance with the same key is being tracked
Hi, so I have following code: Basically I get a batch to update the field
I get the error on the 'entry' line. If I leave it, it doesn't update the field (yes I do commit later on in my CQRS).
I have a code freeze tomorrow night so henc ethe Urgency as It's an imprtant feature the client wants which they only let us know very late...
Full error
26 Replies
Any reason you're updating this way? Usually it's
or better yet,
Also, the only time when I was ever met with this error, is when something fucky-wucky happened with asynchronous code
So some
async void
method, an unawaited task, or other stuff like thatnever heared of the second one, will give that a shot, but the first one did not work as the value didn't get updated
What type is
batch.Status
?Enum
And the db you're using?
SQL Server
never had problems with the Enum before though in case you're wondering
Yeah, it should work
I'm asking because NpgSQL had issues tracking array columns for example, so thought it might be similar
But an enum is straightforward
Okay that was stupidly easy and I somehow never came across this solution on the internet, Even AI didn't suggest it. I don't understand how people trust AI soo much with their code but oh well, you're a legend for helping me out
What was the issue?
ExecuteUpdateAsync worked fine
Nice
but anything else I tried had tracking issues or just didn't update at all
I'd still check the project for wonky async code
This worked because it does the update in a single query
As opposed to fetching the entity and then saving it
it's all async so yeah
When it's two separate queries, something can creep into the middle
Especially with some stray
async void
controller somewhere
Etcforeach (var b in request.Notification.ChangedBatches)
{
await _uow.BatchRepository.UpdateBatchStatus(b.BatchId, Status.AwaitConfirmation);
}
I do await it, I did use async task etc so yeah, no clue
Yeah, this is fine
But it could be async code anywhere else
I'd at least run a cursory solution-wide search for
async void
which groupmate broke my code 😄
The frontend does have it but that shouldn't really be it right?
Should have no impact on the backend, yeah
yeah backend does not have any async voids
Oh well, that's not it, then
If it works it works though, that's what matters lol
Exactly! and it saves me some lines of code, also a +
Yep, the
.Execute...()
methods are probably my favourite addition to EF CoreI really never saw them before. But imagine school actually explaining some USEFULL stuff 😄 altho I didn't see it while doing research so that's also on me
They're fairly new, fwiw
EF Core 7 or thereabouts
okay fair enough, this is teh first year we're allowed to use .NET 7 even
before they kept using .net 5
anyways, thank you very much again!