C
C#11mo ago
Gipper

Edit view not editing the Db entry but instead creating new one

What the title says, when I hit the submit button on the Edit view of my Utilizador model class, it goes to the HTTPPost Edit method in the controller and triggers these lines:
//string aspNetUserId = User.FindFirstValue(ClaimTypes.NameIdentifier);
//string? aspNetUserEmail = _context.Users.Single(user => user.Id == aspNetUserId).Email;
//userPretendidoId = _context.Utilizadores.First(_utilizador => _utilizador.Email == aspNetUserEmail).UtilizadorId;
//utilizador = _context.Utilizadores.First(user=>user.UtilizadorId == userPretendidoId);
_context.Update(utilizador);
await _context.SaveChangesAsync();
//string aspNetUserId = User.FindFirstValue(ClaimTypes.NameIdentifier);
//string? aspNetUserEmail = _context.Users.Single(user => user.Id == aspNetUserId).Email;
//userPretendidoId = _context.Utilizadores.First(_utilizador => _utilizador.Email == aspNetUserEmail).UtilizadorId;
//utilizador = _context.Utilizadores.First(user=>user.UtilizadorId == userPretendidoId);
_context.Update(utilizador);
await _context.SaveChangesAsync();
And instead of updating the relevant table row, it simply creates and inserts a new one with the new UserName I passed in through the view. The commented out code was something I added to try and force it to be the utilizador object I want it to be (for when the update method is called), but when I uncomment and use the code it simply does not add anything.
10 Replies
Pobiega
Pobiega11mo ago
EF core? EF will decide on update vs create based on the tracking state of the entity I suspect your edit view is essentially recreating the entire object from scratch, maybe even without the ID, and you then call update on it thats not really how you update entities with EF; the correct way is to fetch the real item and then mutate that object, and call save changes alternatively, you can set the change tracker state of the entity, but doing that means you are trusting your model binding result a bit too much, imho 🙂
Gipper
GipperOP11mo ago
waaaaaaaaaahhhhhhwaaaaaaaaaahhhhhhwaaaaaaaaaahhhhhhwaaaaaaaaaahhhhhhwaaaaaaaaaahhhhhh Why is it called Edit, then? I guess it's my bad for assuming Okay, what does Update do anyway?
Angius
Angius11mo ago
It tries to patch the old object with new object data. It tends to be a bit fucky-wucky depending on whether the PK is there in the new object or not Prefer either load-edit-save, or .ExecuteUpdateAsync() The former being two database calls:
var thing = await _ctx.Things.FirstOrDefaultAsync(t => t.Id == id);
thing.Name = "Bob";
thing.Count = thing.Count + 5;
await _ctx.SaveChangesAsync();
var thing = await _ctx.Things.FirstOrDefaultAsync(t => t.Id == id);
thing.Name = "Bob";
thing.Count = thing.Count + 5;
await _ctx.SaveChangesAsync();
And the latter being one database call
var rows = await _ctx.Things
.Where(t => t.Id == id)
.ExecuteUpdateAsync(setters => setters
.SetProperty(t => t.Name, "Bob")
.SetProperty(t => t.Count, t => t.Count + 5)
);
var rows = await _ctx.Things
.Where(t => t.Id == id)
.ExecuteUpdateAsync(setters => setters
.SetProperty(t => t.Name, "Bob")
.SetProperty(t => t.Count, t => t.Count + 5)
);
Pobiega
Pobiega11mo ago
its not?
Gipper
GipperOP11mo ago
The view and the method that code was in is called Edit...
Angius
Angius11mo ago
Because that's what they're supposed to do. But the EF method is not called Edit
Pobiega
Pobiega11mo ago
^ In your snippet there is no reference to any "Edit". I can't comment on anything outside of what you have shared or what EF brings.
Angius
Angius11mo ago
In any case, use one of the methods I mentioned instead of .Update() and it's 99% sure it'll fix the issue Unless your controller action is async void or something Then you have other issues @Gipper
Gipper
GipperOP11mo ago
ok, thank y'all again @ZZZZZZZZZZZZZZZZZZZZZZZZZ it's like the 4th time you've helped me this week I really appreciate you ❤️ @ZZZZZZZZZZZZZZZZZZZZZZZZZ the clip in your bio did hurt my brain, well done!
Angius
Angius11mo ago
Another person experiences the pepeloni lol
Want results from more Discord servers?
Add your server