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

IsExcludedFromMigrations() on a foreign key reads a design-time-only annotation. The runtime model (RuntimeForeignKey) omits migration-exclusion data, so the method throws to prevent a silent false default when the caller actually needs design-time information.

Solutions

  1. Obtain the design-time model via IDesignTimeModel and read FK exclusion from there.
  2. Guard with 'is RuntimeForeignKey' before calling and branch to design-time model.
  3. Keep FK migration reads inside design-time-only code paths.

Example fix

// before
var fks = ctx.Model.GetEntityTypes().SelectMany(e => e.GetForeignKeys());
var excluded = fks.First().IsExcludedFromMigrations();
// after
var dtm = ctx.GetService<IDesignTimeModel>().Model;
var fks = dtm.GetEntityTypes().SelectMany(e => e.GetForeignKeys());
var excluded = fks.First().IsExcludedFromMigrations();
Defensive patterns

Strategy: type-guard

Validate before calling

IModel model = ctx.GetService<IDesignTimeModel>().Model;
var excluded = fkFromDesignTimeModel.IsExcludedFromMigrations();

Type guard

using Microsoft.EntityFrameworkCore.Metadata.Internal;
static bool IsRuntime(IForeignKey fk) => fk is RuntimeForeignKey;
static bool SafeIsExcluded(IForeignKey fk)
    => fk is RuntimeForeignKey ? false : fk.IsExcludedFromMigrations();

Try / catch

try { return foreignKey.IsExcludedFromMigrations(); }
catch (InvalidOperationException) when (foreignKey is RuntimeForeignKey)
{ return false; }

Prevention

When it happens

Trigger: Calling foreignKey.IsExcludedFromMigrations() where foreignKey is a RuntimeForeignKey obtained from the runtime model at query time.

Common situations: Migration or scaffolding code accidentally bound to DbContext.Model (runtime); shared utilities that accept either model kind; introspecting FK migration behavior at runtime.

Related errors


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

Appendix: 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 3a2006ef56)