dotnet/efcore · error · InvalidOperationException

The object ' ' has been removed from the model.

Error message

The object '{name}' has been removed from the model.

What it means

Thrown when accessing the Builder of an InternalSequence after it (or its model) has been removed. The Sequence retains its name for the message but its _builder is null once detached.

Solutions

  1. Check IsInModel on the sequence before accessing Builder.
  2. Do not retain sequence builder references across model mutations; re-resolve via model.FindSequence.
  3. Avoid removing a sequence while a convention is mid-flight against it.
  4. Recreate the model instead of reusing detached metadata.

Example fix

// before
var seq = modelBuilder.HasSequence<long>("OrderSeq");
modelBuilder.Ignore("OrderSeq");
seq.Builder.IncrementsBy(1); // throws

// after
if (seq.IsInModel)
{
    seq.Builder.IncrementsBy(1);
}
Defensive patterns

Strategy: type-guard

Type guard

static bool SequenceIsLive(ISequence sequence)
    => sequence is IConventionSequence { IsInModel: true };

Prevention

When it happens

Trigger: InternalSequenceBuilder.Builder get when _builder is null. Triggered by holding an InternalSequence reference after the sequence was removed from the model (model.RemoveSequence, Ignore, or model finalization that drops it) and then accessing its Builder.

Common situations: Convention or pre-validation code caching a sequence builder; HasSequence invoked twice with intervening removal; model caching scenarios where the sequence is dropped but a reference persists; tests that mutate the model and reuse handles.

Related errors


AI-assisted analysis of dotnet/efcore@3a2006ef56 (2026-08-11). Data as JSON: /api/errors/64d1d043a17eeb6f. Report an issue: GitHub.

Appendix: source

Thrown at src/EFCore.Relational/Metadata/Internal/Sequence.cs:236

        }

        var mutableSequence = (Sequence)sequence;
        sequences.Remove((name, schema));
        mutableSequence.SetRemovedFromModel();

        return mutableSequence;
    }

    /// <summary>
    ///     This is an internal API that supports the Entity Framework Core infrastructure and not subject to
    ///     the same compatibility standards as public APIs. It may be changed or removed without notice in
    ///     any release. You should only use it directly in your code with extreme caution and knowing that
    ///     doing so can result in application failures when updating to a new Entity Framework Core release.
    /// </summary>
    public virtual InternalSequenceBuilder Builder
    {
        [DebuggerStepThrough]
        get => _builder ?? throw new InvalidOperationException(CoreStrings.ObjectRemovedFromModel(Name));
    }

    /// <summary>
    ///     This is an internal API that supports the Entity Framework Core infrastructure and not subject to
    ///     the same compatibility standards as public APIs. It may be changed or removed without notice in
    ///     any release. You should only use it directly in your code with extreme caution and knowing that
    ///     doing so can result in application failures when updating to a new Entity Framework Core release.
    /// </summary>
    public virtual bool IsInModel
        => _builder is not null;

    /// <summary>
    ///     This is an internal API that supports the Entity Framework Core infrastructure and not subject to
    ///     the same compatibility standards as public APIs. It may be changed or removed without notice in
    ///     any release. You should only use it directly in your code with extreme caution and knowing that
    ///     doing so can result in application failures when updating to a new Entity Framework Core release.
    /// </summary>
    public virtual void SetRemovedFromModel()

View on GitHub (pinned to 3a2006ef56)