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

IReadOnlyForeignKey.IsExcludedFromMigrations() throws CoreStrings.RuntimeModelMissingData when the foreign key is a RuntimeForeignKey, because the migrations-exclusion annotation is design-time only (RelationalForeignKeyExtensions.cs:299).

Source

Thrown at src/EFCore.Relational/Extensions/RelationalForeignKeyExtensions.cs:299

    ///         not used in application code.
    ///     </para>
    /// </summary>
    /// <param name="foreignKey">The foreign key.</param>
    /// <param name="storeObject">The identifier of the containing store object.</param>
    /// <returns>The foreign key if found, or <see langword="null" /> if none was found.</returns>
    public static IForeignKey? FindSharedObjectRootForeignKey(
        this IForeignKey foreignKey,
        in StoreObjectIdentifier storeObject)
        => (IForeignKey?)((IReadOnlyForeignKey)foreignKey).FindSharedObjectRootForeignKey(storeObject);

    /// <summary>
    ///     Returns a value indicating whether the foreign key constraint is excluded from migrations.
    /// </summary>
    /// <param name="foreignKey">The foreign key.</param>
    /// <returns><see langword="true" /> if the foreign key constraint is excluded from migrations.</returns>
    public static bool IsExcludedFromMigrations(this IReadOnlyForeignKey foreignKey)
        => foreignKey is RuntimeForeignKey
            ? throw new InvalidOperationException(CoreStrings.RuntimeModelMissingData)
            : (bool?)foreignKey[RelationalAnnotationNames.IsForeignKeyExcludedFromMigrations] ?? false;

    /// <summary>
    ///     Sets a value indicating whether the foreign key constraint is excluded from migrations.
    /// </summary>
    /// <param name="foreignKey">The foreign key.</param>
    /// <param name="excluded">The value to set.</param>
    public static void SetIsExcludedFromMigrations(this IMutableForeignKey foreignKey, bool? excluded)
        => foreignKey.SetOrRemoveAnnotation(RelationalAnnotationNames.IsForeignKeyExcludedFromMigrations, excluded);

    /// <summary>
    ///     Sets a value indicating whether the foreign key constraint is excluded from migrations.
    /// </summary>
    /// <param name="foreignKey">The foreign key.</param>
    /// <param name="excluded">The value to set.</param>
    /// <param name="fromDataAnnotation">Indicates whether the configuration was specified using a data annotation.</param>
    /// <returns>The configured value.</returns>
    public static bool? SetIsExcludedFromMigrations(

View on GitHub (pinned to dbf9771522)

Solutions

  1. Read FK exclusion from the design-time model: dbContext.GetService<IDesignTimeModel>().Model.
  2. Reserve IsExcludedFromMigrations calls for the migrations/scaffolding pipeline that operates on the mutable design-time model.

Example fix

// before
var fkExcluded = dbContext.Model.GetEntityTypes()
    .SelectMany(e => e.GetForeignKeys())
    .First().IsExcludedFromMigrations();

// after
var designModel = dbContext.GetService<IDesignTimeModel>().Model;
var fkExcluded = designModel.GetEntityTypes()
    .SelectMany(e => e.GetForeignKeys())
    .First().IsExcludedFromMigrations();
Defensive patterns

Strategy: validation

Validate before calling

var model = dbContext.GetService<IDesignTimeModel>().Model;
var fkExcluded = model.GetEntityTypes()
    .SelectMany(e => e.GetForeignKeys())
    .First().IsExcludedFromMigrations();

Type guard

static bool IsDesignTime(IReadOnlyForeignKey fk) => fk is not RuntimeForeignKey;

Prevention

When it happens

Trigger: Iterating DbContext.Model entity types' foreign keys at runtime and calling IsExcludedFromMigrations().

Common situations: Custom migration planners, schema-diff tools, or admin diagnostics that walk runtime-model foreign keys; sharing a metadata-reading helper between design and runtime.

Related errors


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