{"record":{"id":"d6c923230af8c6e2","repo":"stride3d/stride","slug":"the-entity-to-insert-has-already-been-destroyed","errorCode":null,"errorMessage":"The entity to insert has already been destroyed","messagePattern":"The entity to insert has already been destroyed","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"sources/editor/Stride.Assets.Presentation/AssetEditors/EntityHierarchyEditor/ViewModels/EntityHierarchyItemViewModel.cs","lineNumber":230,"sourceCode":"\n                // Make sure to mark the position node as overridden if this entity has a base.\n                var positionNode = (IAssetMemberNode)Editor.NodeContainer.GetNode(entity.Transform)[nameof(TransformComponent.Position)];\n                if (positionNode.BaseNode != null)\n                {\n                    positionNode.OverrideContent(true);\n                }\n                // Add newly created entity to the result\n                if (Editor.FindPartViewModel(new AbsoluteId(Asset.Id, entity.Id)) is EntityViewModel viewmodel)\n                    result.Add(viewmodel);\n            }\n\n            return result;\n        }\n\n        internal void InsertEntityViewModel([NotNull] EntityViewModel entity, int index, bool expand = true)\n        {\n            if (entity.IsDestroyed)\n                throw new InvalidOperationException(\"The entity to insert has already been destroyed\");\n\n            // Add the view model first, so actual entities can be fetched\n            if (index < 0)\n            {\n                subEntities.Add(entity);\n            }\n            else\n            {\n                subEntities.Insert(index, entity);\n            }\n\n            if (expand)\n            {\n                IsExpanded = true;\n            }\n        }\n\n        internal static void RemoveEntityViewModel([NotNull] EntityViewModel entity)","sourceCodeStart":212,"sourceCodeEnd":248,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/editor/Stride.Assets.Presentation/AssetEditors/EntityHierarchyEditor/ViewModels/EntityHierarchyItemViewModel.cs#L212-L248","documentation":"EntityHierarchyItemViewModel.InsertEntityViewModel refuses to insert an EntityViewModel flagged as destroyed (IsDestroyed), since re-parenting a destroyed view-model would corrupt the hierarchy. It throws InvalidOperationException before any mutation.","triggerScenarios":"Calling InsertEntityViewModel with an entity that was previously destroyed (e.g. via Destroy() or an undo/redo delete) but whose reference is still held and reused.","commonSituations":"Caching entity view-models across delete operations; undo/redo scripts that reinsert entities captured before destruction; double-processing of a delete-then-insert sequence.","solutions":["Create a fresh entity instead of reinserting a destroyed one","Check entity.IsDestroyed before calling InsertEntityViewModel and skip or recreate","Fix undo/redo logic to recreate view-models rather than reuse destroyed instances"],"exampleFix":"// before\nparent.InsertEntityViewModel(entity, index); // throws if destroyed\n// after\nif (!entity.IsDestroyed)\n    parent.InsertEntityViewModel(entity, index);","handlingStrategy":"validation","validationCode":"if (!entity.IsDestroyed)\n    parent.InsertEntityViewModel(entity, index);","typeGuard":"bool IsInsertable(EntityViewModel e) => !e.IsDestroyed;","tryCatchPattern":"try { parent.InsertEntityViewModel(entity, index); }\ncatch (InvalidOperationException) { /* recreate the entity view-model */ }","preventionTips":["Drop references to destroyed view-models immediately","Never reuse entity view-models across delete/undo cycles"],"tags":["csharp","stride","viewmodel","lifecycle"],"backgroundTag":"invalid-state-transition","analyzedSha":"96fad776d210c221682aac1ccdf4c79dc046fc38","analyzedAt":"2026-09-14T02:59:31.279Z","contentChangedAt":"2026-09-14T02:59:31.279Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}