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.
No description
No description
No description
17 Replies
_dawidbartniczak
_dawidbartniczakOP11mo ago
Blazor code trigerring the update
No description
Angius
Angius11mo ago
Impossible to tell without seeing any code, but usually tracking issues stem from: * Fucky-wucky async code, like async void methods or unawaited tasks * newing up the DbContext instead of Ah, didn't even need to finish lmao You got an async void there
_dawidbartniczak
_dawidbartniczakOP11mo ago
Code in the repositories
No description
No description
Jimmacle
Jimmacle11mo ago
async void bonk that can cause issues with blazor itself, not just EF
_dawidbartniczak
_dawidbartniczakOP11mo ago
Changed the async void to Task Now it does not throw errors But the OrderPositions are not created
Jimmacle
Jimmacle11mo ago
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
_dawidbartniczak
_dawidbartniczakOP11mo ago
I have the DbContext lifetime set to Transient Shoud i switch to the DbContext factory?
Jimmacle
Jimmacle11mo ago
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
_dawidbartniczak
_dawidbartniczakOP11mo ago
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
Jimmacle
Jimmacle11mo ago
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
_dawidbartniczak
_dawidbartniczakOP11mo ago
No description
Jimmacle
Jimmacle11mo ago
that doesn't do anything that will help you here
_dawidbartniczak
_dawidbartniczakOP11mo ago
Ok i will have to rethink that code
Jimmacle
Jimmacle11mo ago
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
_dawidbartniczak
_dawidbartniczakOP11mo ago
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
Freeman
Freeman11mo ago
It will fail every 2nd run. Because of the UpdatePositionAsync method order.OrderPositions = order.OrderPositions.Orderby... What is the purpose here?
_dawidbartniczak
_dawidbartniczakOP11mo ago
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
Want results from more Discord servers?
Add your server