DbConcurrencyException, what am I missing?
Hi, getting this on the SaveChangesAsync and im not really sure what im doing wrong.
DbConcurrencyException: The database operation was expected to affect 1 row(s), but actually affected 0 row(s); data may have been modified or deleted since entities were loaded.
Inside UserService.cs:
Inside User.cs:
Inside UserRepository.cs:
32 Replies
Also to mention, I do include Variables when getting the user and I also tried without the explicit Update call
I generally avoid using
.Update()
And using repositories, but that's another matter
See if using .ExecuteUpdateAsync()
would fix the issueah, the joys of wrapping EF in generic repositories
ðŸ˜
which you shouldn't if you have a choice
Why do "best practices" youtube channels teach this stuff
¯\_(ツ)_/¯
lots of useless content out there
anyway
is this how you use it?
It doesnt seem to work
that would set those variables on every single user in the table
you didn't filter it beforehand
ah
i'm not actually sure how executeupdate works with navigation properties, i haven't tried it
is there another possible way I could fix the issue?
as far as options go, you can load, modify, then save the user and let the change tracker deal with it
if you can tolerate the performance penalty of 2 round trips
yeah this isnt very performance dependant
thats kinda what I tried to do I guess
loading the user, modifying it's Variables collection and then trying to save the user
you don't need to use
Update
if the object is already being tracked by the dbcontextyeah that makes sense
but UserVariable has its own table as well
that's fine, the change tracker can deal with that
But even without the
Update
its giving me the DbUpdateConcurrencyException
It feels like it should work..?
are you somehow changing the ID of the user (or variable) either in the entity or in the db in the middle of this happening?
yes the variable is recieving a new ID
so you need to insert, not update
if you change the ID then
WHERE [Id] = @p3;
is never going to find the old oneah
but thats weird
so I need to insert the userVariable to it's table before I add it to the user collection
no, it basically means you're doing something in your EF model that is leading it to generate the wrong SQL for what actually needs to happen
i see
I honestly have no clue what it could be
In the other cases I have repositories for every model that im working with
so I just add it via reposistory before adding it to another model's collection
but in this case I thought it would be unnecessary
how can I check if its handled by change tracker btw?
if you got the object from a query on the same dbcontext (that didn't use AsNoTracking), explicitly added it with Add, or is a member of an object that meets those conditions, it's handled by the change tracker
ah
yeah I just get it like this
then all you should have to do is add to the user's variables collection and call savechanges, you don't even need to explicitly set the reference to the user
damn
the change tracker will figure that out if it sees a new item in the user's variables collection
could it be the way i configured the table?
i have no clue how to find the issue ðŸ˜
I tried to drop the database and have EF core re create it
but still same
Well I found the cause
Value generation was turned on even though I set the Id property manually on creation
so I had to add a .ValueGeneratedNever()
alternatively had to manually insert by calling DbContext.Add but since I handle the id creations myself I just set the .ValueGeneratedNever and now it seems to work fine