admin: fix RemoveCard DbUpdateConcurrencyException on save
PrebuiltDeckCard.Id is always a NewGuid() (never Guid.Empty), so the old `if (entry.Id != Guid.Empty) Db.PrebuiltDeckCards.Remove(entry)` guard was always true. When a user added a card then removed it before saving, EF detached the Added entity from the collection, then the explicit Db.Remove re-attached it as Deleted. SaveChangesAsync then tried to DELETE a row that was never inserted → DbUpdateConcurrencyException. Drop the explicit remove call entirely. EF Core automatically marks Unchanged orphans as Deleted when removed from a required relationship collection, and simply detaches Added orphans — both are the correct outcomes without any extra Db.Remove. Generated with [Claude Code](https://claude.ai/code) via [Happy](https://happy.engineering) Co-Authored-By: Claude <noreply@anthropic.com> Co-Authored-By: Happy <yesreply@happy.engineering>
This commit is contained in:
@@ -377,8 +377,10 @@ else
|
||||
private void RemoveCard(PrebuiltDeckCard entry)
|
||||
{
|
||||
_deck!.Cards.Remove(entry);
|
||||
if (entry.Id != Guid.Empty)
|
||||
Db.PrebuiltDeckCards.Remove(entry);
|
||||
// EF automatically marks Unchanged cards as Deleted when removed from a required
|
||||
// relationship collection (client-side cascade). For Added (unsaved) cards, EF
|
||||
// detaches them — calling Db.Remove on a detached entity would re-attach it as
|
||||
// Deleted and cause a 0-rows-affected DbUpdateConcurrencyException on save.
|
||||
}
|
||||
|
||||
private async Task SaveAsync()
|
||||
|
||||
Reference in New Issue
Block a user