stride3d/stride · error · InvalidOperationException

Cannot add an entity to this entity manager when it is…

Error message

Cannot add an entity to this entity manager when it is already used by another entity manager

What it means

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.

Solutions

  1. Remove the entity from its current EntityManager before adding it here (oldManager.Remove(entity))
  2. Create a new entity instance for the new manager
  3. Guard with a check: if (entity.EntityManager == null) before adding

Example fix

// before
newSceneManager.EntityManager.Add(entity);
// after
if (entity.EntityManager != null && entity.EntityManager != newSceneManager.EntityManager)
    entity.EntityManager.Remove(entity);
newSceneManager.EntityManager.Add(entity);
Defensive patterns

Strategy: validation

Validate before calling

if (entity.EntityManager != null && entity.EntityManager != targetManager)
    entity.EntityManager.Remove(entity);
targetManager.InternalAddOrSkip(entity); // public Add path

Type guard

static bool IsUnmanaged(Stride.Engine.Entity e) => e?.EntityManager == null;

Try / catch

try { entityManager.Add(entity); }
catch (InvalidOperationException ex) when (ex.Message.Contains("another entity manager")) { /* remove from old manager first */ }

Prevention

When it happens

Trigger: Adding an entity that is currently registered with another EntityManager instance; sharing entities between two scenes/managers.

Common situations: 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.

Understand the failure class

Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.

Related errors


AI-assisted analysis of stride3d/stride@96fad776d2 (2026-09-14). Data as JSON: /api/errors/4c9d0c5585ad863a. Report an issue: GitHub.

Appendix: source

Thrown at sources/engine/Stride.Engine/Engine/EntityManager.cs:240

            if (entity.Transform != null && entity.Transform.Parent != null)
                throw new ArgumentException("Entity shouldn't have a parent.", nameof(entity));

            InternalAddEntity(entity);
        }

        /// <summary>
        /// Adds the specified entity.
        /// </summary>
        /// <param name="entity">The entity to add.</param>
        internal void InternalAddEntity(Entity entity)
        {
            // Already added?
            if (entities.Contains(entity))
                return;

            if (entity.EntityManager != null)
            {
                throw new InvalidOperationException("Cannot add an entity to this entity manager when it is already used by another entity manager");
            }

            // Add this entity to our internal hashset
            entity.EntityManager = this;
            entities.Add(entity);
            entity.AddReferenceInternal();

            // Because a processor can add entities, we want to make sure that 
            // the RegisterPendingProcessors is called only at the top level
            {
                addEntityLevel++;

                // Check which exiting processor are working with the components of this entity
                // and grab the list of new processors to registers
                CheckEntityWithProcessors(entity, false, true);

                addEntityLevel--;
            }

View on GitHub (pinned to 96fad776d2)