dotnet/efcore · error · InvalidOperationException

The requested configuration is not stored in the read-optimi

Error message

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

What it means

IReadOnlyProperty.GetColumnOrder() throws CoreStrings.RuntimeModelMissingData on a RuntimeProperty because column order is a design-time annotation (RelationalPropertyExtensions.cs:396).

Source

Thrown at src/EFCore.Relational/Extensions/RelationalPropertyExtensions.cs:396

    /// <summary>
    ///     Gets the <see cref="ConfigurationSource" /> for the column name for a particular table-like store object.
    /// </summary>
    /// <param name="property">The property.</param>
    /// <param name="storeObject">The identifier of the table-like store object containing the column.</param>
    /// <returns>The <see cref="ConfigurationSource" /> for the column name for a particular table-like store object.</returns>
    public static ConfigurationSource? GetColumnNameConfigurationSource(
        this IConventionProperty property,
        in StoreObjectIdentifier storeObject)
        => property.FindOverrides(storeObject)?.GetColumnNameConfigurationSource();

    /// <summary>
    ///     Returns the order of the column this property is mapped to.
    /// </summary>
    /// <param name="property">The property.</param>
    /// <returns>The column order.</returns>
    public static int? GetColumnOrder(this IReadOnlyProperty property)
        => (property is RuntimeProperty)
            ? throw new InvalidOperationException(CoreStrings.RuntimeModelMissingData)
            : (int?)property.FindAnnotation(RelationalAnnotationNames.ColumnOrder)?.Value;

    /// <summary>
    ///     Returns the order of the column this property is mapped to for a particular table.
    /// </summary>
    /// <param name="property">The property.</param>
    /// <param name="storeObject">The identifier of the table-like store object containing the column.</param>
    /// <returns>The column order.</returns>
    public static int? GetColumnOrder(this IReadOnlyProperty property, in StoreObjectIdentifier storeObject)
    {
        if (property is RuntimeProperty)
        {
            throw new InvalidOperationException(CoreStrings.RuntimeModelMissingData);
        }

        var annotation = property.FindAnnotation(RelationalAnnotationNames.ColumnOrder);
        if (annotation != null)
        {

View on GitHub (pinned to dbf9771522)

Solutions

  1. Read from the design-time model: dbContext.GetService<IDesignTimeModel>().Model.
  2. Use GetColumnOrder during migrations/scaffolding, not in runtime application code.

Example fix

// before
var order = dbContext.Model.GetEntityTypes()
    .First().GetProperties().First().GetColumnOrder();

// after
var order = dbContext.GetService<IDesignTimeModel>().Model
    .GetEntityTypes().First().GetProperties().First().GetColumnOrder();
Defensive patterns

Strategy: validation

Validate before calling

var model = dbContext.GetService<IDesignTimeModel>().Model;
var order = model.GetEntityTypes().First().GetProperties().First().GetColumnOrder();

Type guard

static bool IsDesignTime(IProperty p) => p is not RuntimeProperty;

Prevention

When it happens

Trigger: Calling property.GetColumnOrder() on properties from dbContext.Model at runtime.

Common situations: Schema-reporting tools, migration generators, or DDL emitters reading column ordering from the runtime model.

Related errors


AI-assisted analysis of dotnet/efcore@dbf9771522 (2026-08-06). Data as JSON: /api/errors/7c8c40dda63f2b2c. Report an issue: GitHub.