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 InternalStoredProcedure after the stored procedure mapping has been removed from the model. The _builder field is null once detached.

Solutions

  1. Check IsInModel before accessing Builder.
  2. Do not cache stored procedure builder references across mutations; re-resolve via EntityType.FindStoredProcedure.
  3. Make sure the entity still has the stored procedure mapping before reconfiguring.
  4. Rebuild the model rather than reusing detached metadata.

Example fix

// before
var sprocBuilder = entityTypeBuilder.Metadata.FindUpdateStoredProcedure()!.Builder;
entityTypeBuilder.Metadata.RemoveStoredProcedure();
sprocBuilder.HasParameter(...); // throws

// after
if (sprocBuilder.IsInModel)
{
    sprocBuilder.HasParameter(...);
}
Defensive patterns

Strategy: type-guard

Type guard

static bool StoredProcedureIsLive(IConventionStoredProcedure sproc)
    => sproc is { IsInModel: true };

Prevention

When it happens

Trigger: InternalStoredProcedureBuilder.Builder get when _builder is null. Triggered by retaining a stored procedure builder reference after the mapping was removed (entity ignored, mapping cleared, model rebuild) and then re-accessing Builder.

Common situations: Conventions or model configurators caching a stored procedure builder across mutations; calling configuration twice after an intervening removal; tests mutating the model and reusing handles.

Related errors


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

Appendix: source

Thrown at src/EFCore.Relational/Metadata/Internal/StoredProcedure.cs:62

    {
        EntityType = entityType;
        _configurationSource = configurationSource;
        _builder = new InternalStoredProcedureBuilder(this, ((IConventionEntityType)entityType).Model.Builder);
    }

    /// <inheritdoc />
    public virtual IMutableEntityType EntityType { get; set; }

    /// <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 InternalStoredProcedureBuilder 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)