From 0607f02e93791fc14cdc263952ba021b7547bc6a Mon Sep 17 00:00:00 2001 From: dingo Date: Sun, 1 Mar 2026 09:22:38 -0500 Subject: [PATCH] admin: fix RemoveCard DbUpdateConcurrencyException on save MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 Co-Authored-By: Happy --- .../Components/Pages/Bazaar/PrebuiltDeckEditor.razor | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Server/src/SOR.Admin/Components/Pages/Bazaar/PrebuiltDeckEditor.razor b/Server/src/SOR.Admin/Components/Pages/Bazaar/PrebuiltDeckEditor.razor index 82e7412..16d711e 100644 --- a/Server/src/SOR.Admin/Components/Pages/Bazaar/PrebuiltDeckEditor.razor +++ b/Server/src/SOR.Admin/Components/Pages/Bazaar/PrebuiltDeckEditor.razor @@ -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()