{"record":{"id":"51e2ae9864f49341","repo":"stride3d/stride","slug":"this-entity-s-scene-is-not-the-expected-value","errorCode":null,"errorMessage":"This entity's scene is not the expected value.","messagePattern":"This entity's scene is not the expected value\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"sources/engine/Stride.Engine/Engine/Scene.cs","lineNumber":138,"sourceCode":"            }\n\n            /// <inheritdoc/>\n            protected override void InsertItem(int index, Entity item)\n            {\n                // Root entity in another scene, or child of another entity\n                if (item.Scene != null)\n                    throw new InvalidOperationException(\"This entity already has a scene. Detach it first.\");\n\n                item.SceneValue = scene;\n                base.InsertItem(index, item);\n            }\n\n            /// <inheritdoc/>\n            protected override void RemoveItem(int index)\n            {\n                var item = this[index];\n                if (item.SceneValue != scene)\n                    throw new InvalidOperationException(\"This entity's scene is not the expected value.\");\n\n                item.SceneValue = null;\n                base.RemoveItem(index);\n            }\n        }\n\n        [DataContract]\n        public class SceneCollection : TrackingCollection<Scene>\n        {\n            Scene scene;\n\n            public SceneCollection(Scene sceneParam)\n            {\n                scene = sceneParam;\n            }\n\n            /// <inheritdoc/>\n            protected override void InsertItem(int index, Scene item)","sourceCodeStart":120,"sourceCodeEnd":156,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/engine/Stride.Engine/Engine/Scene.cs#L120-L156","documentation":"Scene.Entities collection's RemoveItem throws InvalidOperationException when the entity being removed has a SceneValue that does not match the collection's owning scene — an internal bookkeeping invariant violation.","triggerScenarios":"Removing an entity from a scene's Entities collection when the entity's stored scene reference points elsewhere (e.g. entity was already moved, scene references were manipulated directly, or entity.SceneValue was set/cleared manually).","commonSituations":"Directly mutating SceneValue via reflection/serialization tooling; concurrent modifications of scene membership from two systems; restoring serialized state that sets SceneValue inconsistently; removing the same entity twice across scenes.","solutions":["Only remove entities via the scene that currently owns them (check entity.Scene == scene first)","Do not modify entity.SceneValue directly; go through scene Entities add/remove","Guard removal: if (entity.Scene == scene) scene.Entities.Remove(entity);","Rebuild/repair entity references after deserialization before mutating scene membership"],"exampleFix":"// before\nscene.Entities.Remove(entity); // may belong elsewhere\n// after\nif (entity.Scene == scene)\n    scene.Entities.Remove(entity);","handlingStrategy":"validation","validationCode":"if (entity.Scene == scene) scene.Entities.Remove(entity);","typeGuard":"bool OwnedByScene(Entity e, Scene s) => ReferenceEquals(e.Scene, s);","tryCatchPattern":"try { scene.Entities.Remove(entity); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"not the expected value\")) { /* repair membership bookkeeping */ }","preventionTips":["Never set entity.SceneValue directly; use scene Entities add/remove","Serialize/deserialize scene membership in one consistent pass","Avoid concurrent scene membership mutations from multiple systems","Verify entity.Scene before removing"],"tags":["scene","entity","invariant"],"backgroundTag":"internal-invariant-violation","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"}