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
- Resolve the design-time model: DbContext.GetService<IDesignTimeModel>().Model, and read CheckConstraints from its tables.
- Move check-constraint inspection out of runtime code paths into design-time tooling.
- Cache the design-time model once for the application lifetime when tooling needs it.
- 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
- Reserve check-constraint (and other design-time) inspection for IDesignTimeModel-based tooling.
- Keep runtime code paths off ITable.CheckConstraints.
- Under NativeAOT, always pair the app with a compiled model and verify tooling uses the design-time model.
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
- Model building is not supported when publishing with…
- Model building is not supported when publishing with…
- Model building is not supported when publishing with…
- No type deriving from DbContext was found. Add [assembly…
- Query precompilation failed with 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)