From 5d4f0c37458c78c3db7bbba216a09fa5ede9caff Mon Sep 17 00:00:00 2001 From: dingo Date: Sun, 1 Mar 2026 11:14:06 -0500 Subject: [PATCH] admin: fix CardEditor AddAsset/AddAnimation + Remove EF bugs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Same two classes of bug as PrebuiltDeckEditor: 1. AddAsset / AddAnimation — ValueGeneratedOnAdd sentinel mismatch: New entities with Id = Guid.NewGuid() added to a tracked ObservableHashSet are classified as Modified by EF, causing a 0-rows-affected UPDATE. Fix: Db.Entry(entry).State = EntityState.Added when !_isNew. 2. RemoveAsset / RemoveAnimation — redundant Db.Remove on detached entity: Id != Guid.Empty is always true (Guid.NewGuid() default). Removing an Added entity from the collection detaches it; calling Db.Remove re-attaches it as Deleted → DELETE on a non-existent row → DbUpdateConcurrencyException. Fix: removed the explicit Db.Remove calls; EF handles orphan deletion. Generated with [Claude Code](https://claude.ai/code) via [Happy](https://happy.engineering) Co-Authored-By: Claude Co-Authored-By: Happy --- .../Components/Pages/Cards/CardEditor.razor | 32 +++++++++++-------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/Server/src/SOR.Admin/Components/Pages/Cards/CardEditor.razor b/Server/src/SOR.Admin/Components/Pages/Cards/CardEditor.razor index 359e04c..009449d 100644 --- a/Server/src/SOR.Admin/Components/Pages/Cards/CardEditor.razor +++ b/Server/src/SOR.Admin/Components/Pages/Cards/CardEditor.razor @@ -478,31 +478,37 @@ else } private void AddAsset(CardAssetType type) - => _card!.Assets.Add(new CardAsset + { + var entry = new CardAsset { - CardDefinitionId = _card.Id, - AssetType = type, - CreatedAt = DateTimeOffset.UtcNow, - }); + CardDefinitionId = _card!.Id, + AssetType = type, + CreatedAt = DateTimeOffset.UtcNow, + }; + _card.Assets.Add(entry); + if (!_isNew) Db.Entry(entry).State = EntityState.Added; + } private void RemoveAsset(CardAsset asset) { _card!.Assets.Remove(asset); - if (asset.Id != Guid.Empty) - Db.CardAssets.Remove(asset); + // EF automatically marks Unchanged assets as Deleted when removed from a required + // relationship collection. For Added (unsaved) assets EF detaches them — calling + // Db.Remove on a detached entity re-attaches it as Deleted and causes a + // 0-rows-affected DbUpdateConcurrencyException on save. } private void AddAnimation() - => _card!.AnimationBindings.Add(new CardAnimationBinding - { - CardDefinitionId = _card.Id, - }); + { + var entry = new CardAnimationBinding { CardDefinitionId = _card!.Id }; + _card.AnimationBindings.Add(entry); + if (!_isNew) Db.Entry(entry).State = EntityState.Added; + } private void RemoveAnimation(CardAnimationBinding anim) { _card!.AnimationBindings.Remove(anim); - if (anim.Id != Guid.Empty) - Db.AnimationBindings.Remove(anim); + // Same as RemoveAsset: EF handles orphan deletion automatically. } // ── CDN upload helpers ───────────────────────────────────────────────────