EF Core - Tracking problems while updating
I have an Blazor web app (Order Management software). In this app i have a view with all the articles for given supplier and active orders. You can enter the desired amount of each article for each order. While entering the order i get a lot of errors regarding tracking issues. The wired thing is that it works for about 75% of the articles, always for the same ones. For the other ones it always throws an excpetion regarding tracking OrderPosition, wired because it is newly created. The other thing is that after the first insertion all the subsequent ones fail with the exception now beeing about the tracking of Order. When updating the already existing order position it works.
17 Replies
Blazor code trigerring the update
Impossible to tell without seeing any code, but usually tracking issues stem from:
* Fucky-wucky
async
code, like async void
methods or unawaited tasks
* new
ing up the DbContext
instead of
Ah, didn't even need to finish lmao
You got an async void
thereCode in the repositories
async void that can cause issues with blazor itself, not just EF
Changed the async void to Task
Now it does not throw errors
But the OrderPositions are not created
you may also have lifetime issues, it's not clear whether you're using a fresh dbcontext or the """scoped""" instance that shouldn't be used with blazor server
blazor scopes live as long as the tab is open which isn't what you'd expect and is bad for EF since you shouldn't keep the same dbcontext that long
I have the DbContext lifetime set to Transient
Shoud i switch to the DbContext factory?
when in doubt, yes
that's what i was doing before "fixing" my scoping issues with a mediator that creates a new service scope for reach request
Thanks, i will switch to the Context Factory way
But regarding the original issue
After changing to async Task
I do not get any errors but the data is not saving at all
you still need to await the task
which you can't do in a property, so you'll need to find another way to structure that code
that doesn't do anything that will help you here
Ok i will have to rethink that code
for these cases i bind to Value and ValueChanged instead of using the two way binding
then you have an async method where you can properly await things
I will have to do that
But wired that it works for some of the positions
But not for the others
Simmingly randomly
And always for the same positions
Changed to the ValueChanged event
The error has persisted
It will fail every 2nd run. Because of the UpdatePositionAsync method order.OrderPositions = order.OrderPositions.Orderby...
What is the purpose here?
I have managed to fix the issue
Due to multiple passings of the objects between components it was probably a copy not a reference
When retrieving the object fresh before update it works good
Also it is good that the state of the object before update is retreived, the check for the Order finalization state has fresh data