{"record":{"id":"e2b44e6436f853a5","repo":"dotnet/efcore","slug":"the-requested-configuration-is-not-stored-in-the-r-e2b44e","errorCode":null,"errorMessage":"The requested configuration is not stored in the read-optimized model, please use 'DbContext.GetService<IDesignTimeModel>().Model'.","messagePattern":"The requested configuration is not stored in the read-optimized model, please use 'DbContext\\.GetService<IDesignTimeModel>\\(\\)\\.Model'\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/EFCore.Relational/Extensions/RelationalForeignKeyExtensions.cs","lineNumber":299,"sourceCode":"    ///         not used in application code.\n    ///     </para>\n    /// </summary>\n    /// <param name=\"foreignKey\">The foreign key.</param>\n    /// <param name=\"storeObject\">The identifier of the containing store object.</param>\n    /// <returns>The foreign key if found, or <see langword=\"null\" /> if none was found.</returns>\n    public static IForeignKey? FindSharedObjectRootForeignKey(\n        this IForeignKey foreignKey,\n        in StoreObjectIdentifier storeObject)\n        => (IForeignKey?)((IReadOnlyForeignKey)foreignKey).FindSharedObjectRootForeignKey(storeObject);\n\n    /// <summary>\n    ///     Returns a value indicating whether the foreign key constraint is excluded from migrations.\n    /// </summary>\n    /// <param name=\"foreignKey\">The foreign key.</param>\n    /// <returns><see langword=\"true\" /> if the foreign key constraint is excluded from migrations.</returns>\n    public static bool IsExcludedFromMigrations(this IReadOnlyForeignKey foreignKey)\n        => foreignKey is RuntimeForeignKey\n            ? throw new InvalidOperationException(CoreStrings.RuntimeModelMissingData)\n            : (bool?)foreignKey[RelationalAnnotationNames.IsForeignKeyExcludedFromMigrations] ?? false;\n\n    /// <summary>\n    ///     Sets a value indicating whether the foreign key constraint is excluded from migrations.\n    /// </summary>\n    /// <param name=\"foreignKey\">The foreign key.</param>\n    /// <param name=\"excluded\">The value to set.</param>\n    public static void SetIsExcludedFromMigrations(this IMutableForeignKey foreignKey, bool? excluded)\n        => foreignKey.SetOrRemoveAnnotation(RelationalAnnotationNames.IsForeignKeyExcludedFromMigrations, excluded);\n\n    /// <summary>\n    ///     Sets a value indicating whether the foreign key constraint is excluded from migrations.\n    /// </summary>\n    /// <param name=\"foreignKey\">The foreign key.</param>\n    /// <param name=\"excluded\">The value to set.</param>\n    /// <param name=\"fromDataAnnotation\">Indicates whether the configuration was specified using a data annotation.</param>\n    /// <returns>The configured value.</returns>\n    public static bool? SetIsExcludedFromMigrations(","sourceCodeStart":281,"sourceCodeEnd":317,"githubUrl":"https://github.com/dotnet/efcore/blob/3a2006ef569de08368d59db5e1468aa8f407e4f8/src/EFCore.Relational/Extensions/RelationalForeignKeyExtensions.cs#L281-L317","documentation":"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.","triggerScenarios":"Calling foreignKey.IsExcludedFromMigrations() where foreignKey is a RuntimeForeignKey obtained from the runtime model at query time.","commonSituations":"Migration or scaffolding code accidentally bound to DbContext.Model (runtime); shared utilities that accept either model kind; introspecting FK migration behavior at runtime.","solutions":["Obtain the design-time model via IDesignTimeModel and read FK exclusion from there.","Guard with 'is RuntimeForeignKey' before calling and branch to design-time model.","Keep FK migration reads inside design-time-only code paths."],"exampleFix":"// before\nvar fks = ctx.Model.GetEntityTypes().SelectMany(e => e.GetForeignKeys());\nvar excluded = fks.First().IsExcludedFromMigrations();\n// after\nvar dtm = ctx.GetService<IDesignTimeModel>().Model;\nvar fks = dtm.GetEntityTypes().SelectMany(e => e.GetForeignKeys());\nvar excluded = fks.First().IsExcludedFromMigrations();","handlingStrategy":"type-guard","validationCode":"IModel model = ctx.GetService<IDesignTimeModel>().Model;\nvar excluded = fkFromDesignTimeModel.IsExcludedFromMigrations();","typeGuard":"using Microsoft.EntityFrameworkCore.Metadata.Internal;\nstatic bool IsRuntime(IForeignKey fk) => fk is RuntimeForeignKey;\nstatic bool SafeIsExcluded(IForeignKey fk)\n    => fk is RuntimeForeignKey ? false : fk.IsExcludedFromMigrations();","tryCatchPattern":"try { return foreignKey.IsExcludedFromMigrations(); }\ncatch (InvalidOperationException) when (foreignKey is RuntimeForeignKey)\n{ return false; }","preventionTips":["Read FK migration exclusion only from the design-time model.","Guard shared utilities with a RuntimeForeignKey check.","Keep migration-related FK reads out of runtime code paths."],"tags":["runtime-model","design-time","annotations","foreign-key","migrations"],"backgroundTag":null,"analyzedSha":"3a2006ef569de08368d59db5e1468aa8f407e4f8","analyzedAt":"2026-08-11T23:42:04.146Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}