{"record":{"id":"4c9d0c5585ad863a","repo":"stride3d/stride","slug":"cannot-add-an-entity-to-this-entity-manager-when-it-is","errorCode":null,"errorMessage":"Cannot add an entity to this entity manager when it is already used by another entity manager","messagePattern":"Cannot add an entity to this entity manager when it is already used by another entity manager","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"sources/engine/Stride.Engine/Engine/EntityManager.cs","lineNumber":240,"sourceCode":"            if (entity.Transform != null && entity.Transform.Parent != null)\n                throw new ArgumentException(\"Entity shouldn't have a parent.\", nameof(entity));\n\n            InternalAddEntity(entity);\n        }\n\n        /// <summary>\n        /// Adds the specified entity.\n        /// </summary>\n        /// <param name=\"entity\">The entity to add.</param>\n        internal void InternalAddEntity(Entity entity)\n        {\n            // Already added?\n            if (entities.Contains(entity))\n                return;\n\n            if (entity.EntityManager != null)\n            {\n                throw new InvalidOperationException(\"Cannot add an entity to this entity manager when it is already used by another entity manager\");\n            }\n\n            // Add this entity to our internal hashset\n            entity.EntityManager = this;\n            entities.Add(entity);\n            entity.AddReferenceInternal();\n\n            // Because a processor can add entities, we want to make sure that \n            // the RegisterPendingProcessors is called only at the top level\n            {\n                addEntityLevel++;\n\n                // Check which exiting processor are working with the components of this entity\n                // and grab the list of new processors to registers\n                CheckEntityWithProcessors(entity, false, true);\n\n                addEntityLevel--;\n            }","sourceCodeStart":222,"sourceCodeEnd":258,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/engine/Stride.Engine/Engine/EntityManager.cs#L222-L258","documentation":"InternalAddEntity throws InvalidOperationException when the entity's EntityManager property already points to a different manager, since an entity may be managed by exactly one EntityManager at a time. Entities already contained in this manager return silently, so only cross-manager reuse triggers the error.","triggerScenarios":"Adding an entity that is currently registered with another EntityManager instance; sharing entities between two scenes/managers.","commonSituations":"Moving entities between loaded scenes without removing them from the old manager; tests reusing entity fixtures across manager instances; reloading a scene while keeping old entity objects.","solutions":["Remove the entity from its current EntityManager before adding it here (oldManager.Remove(entity))","Create a new entity instance for the new manager","Guard with a check: if (entity.EntityManager == null) before adding"],"exampleFix":"// before\nnewSceneManager.EntityManager.Add(entity);\n// after\nif (entity.EntityManager != null && entity.EntityManager != newSceneManager.EntityManager)\n    entity.EntityManager.Remove(entity);\nnewSceneManager.EntityManager.Add(entity);","handlingStrategy":"validation","validationCode":"if (entity.EntityManager != null && entity.EntityManager != targetManager)\n    entity.EntityManager.Remove(entity);\ntargetManager.InternalAddOrSkip(entity); // public Add path","typeGuard":"static bool IsUnmanaged(Stride.Engine.Entity e) => e?.EntityManager == null;","tryCatchPattern":"try { entityManager.Add(entity); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"another entity manager\")) { /* remove from old manager first */ }","preventionTips":["One EntityManager owns each entity; remove before transferring","Don't share entities across scenes","After scene reloads, create fresh entities rather than reusing managed ones"],"tags":["csharp","stride","ownership","invalid-state"],"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"}