dotnet/efcore · error · InvalidOperationException

The requested configuration is not stored in the…

Error message

The requested configuration is not stored in the read-optimized model, please use 'DbContext.GetService<IDesignTimeModel>().Model'.

What it means

The read-optimized runtime model omits design-time-only data such as check constraints. When the underlying EntityType is a RuntimeEntityType, the Table.CheckConstraints getter throws, directing you to the design-time model where that metadata lives.

Solutions

  1. Resolve the design-time model: DbContext.GetService<IDesignTimeModel>().Model, and read CheckConstraints from its tables.
  2. Move check-constraint inspection out of runtime code paths into design-time tooling.
  3. Cache the design-time model once for the application lifetime when tooling needs it.
  4. Confirm you are not passing a RuntimeModel where a convention model is expected.

Example fix

// before
var table = dbContext.Model.GetTables().First();
var checks = ((ITable)table).CheckConstraints; // throws in runtime model

// after
var designModel = dbContext.GetService<IDesignTimeModel>().Model;
var table = designModel.GetTables().First();
var checks = ((ITable)table).CheckConstraints;
Defensive patterns

Strategy: validation

Validate before calling

// Use the design-time model for check-constraint inspection.
var designModel = dbContext.GetService<IDesignTimeModel>().Model;
var table = designModel.GetTables().First(t => t.Name == "Orders");
var checks = ((ITable)table).CheckConstraints;

Type guard

static bool IsDesignTimeModel(IModel model)
    => model is not RuntimeModel;

Prevention

When it happens

Trigger: Calling table.CheckConstraints (via the ITable interface) on a Table built from a runtime/read-optimized model (e.g. the model returned by DbContext.Model after the context is finalized, or under NativeAOT compiled models).

Common situations: Tooling, migrations, or scaffolding code that accidentally uses the runtime Model instead of IDesignTimeModel; switching a project to NativeAOT/compiled models; querying check constraints at runtime in a hot path.

Related errors


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

Appendix: source

Thrown at src/EFCore.Relational/Metadata/Internal/Table.cs:222

    IEnumerable<IUniqueConstraint> ITable.UniqueConstraints
    {
        [DebuggerStepThrough]
        get => UniqueConstraints.Values;
    }

    /// <inheritdoc />
    IEnumerable<ITableIndex> ITable.Indexes
    {
        [DebuggerStepThrough]
        get => Indexes.Values;
    }

    /// <inheritdoc />
    IEnumerable<ICheckConstraint> ITable.CheckConstraints
    {
        [DebuggerStepThrough]
        get => EntityTypeMappings.First().TypeBase is RuntimeEntityType
            ? throw new InvalidOperationException(CoreStrings.RuntimeModelMissingData)
            : CheckConstraints.Values;
    }

    /// <inheritdoc />
    IEnumerable<ITrigger> ITable.Triggers
    {
        [DebuggerStepThrough]
        get => Triggers.Values;
    }

    /// <inheritdoc />
    [DebuggerStepThrough]
    IColumn? ITable.FindColumn(string name)
        => (IColumn?)base.FindColumn(name);

    /// <inheritdoc />
    [DebuggerStepThrough]
    IColumn? ITable.FindColumn(IPropertyBase propertyBase)

View on GitHub (pinned to 3a2006ef56)