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 ───────────────────────────────────────────────────