{"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/dbf9771522148d61a2467854921bd5dc6f6e6916/src/EFCore.Relational/Extensions/RelationalForeignKeyExtensions.cs#L281-L317","documentation":"IReadOnlyForeignKey.IsExcludedFromMigrations() throws CoreStrings.RuntimeModelMissingData when the foreign key is a RuntimeForeignKey, because the migrations-exclusion annotation is design-time only (RelationalForeignKeyExtensions.cs:299).","triggerScenarios":"Iterating DbContext.Model entity types' foreign keys at runtime and calling IsExcludedFromMigrations().","commonSituations":"Custom migration planners, schema-diff tools, or admin diagnostics that walk runtime-model foreign keys; sharing a metadata-reading helper between design and runtime.","solutions":["Read FK exclusion from the design-time model: dbContext.GetService<IDesignTimeModel>().Model.","Reserve IsExcludedFromMigrations calls for the migrations/scaffolding pipeline that operates on the mutable design-time model."],"exampleFix":"// before\nvar fkExcluded = dbContext.Model.GetEntityTypes()\n    .SelectMany(e => e.GetForeignKeys())\n    .First().IsExcludedFromMigrations();\n\n// after\nvar designModel = dbContext.GetService<IDesignTimeModel>().Model;\nvar fkExcluded = designModel.GetEntityTypes()\n    .SelectMany(e => e.GetForeignKeys())\n    .First().IsExcludedFromMigrations();","handlingStrategy":"validation","validationCode":"var model = dbContext.GetService<IDesignTimeModel>().Model;\nvar fkExcluded = model.GetEntityTypes()\n    .SelectMany(e => e.GetForeignKeys())\n    .First().IsExcludedFromMigrations();","typeGuard":"static bool IsDesignTime(IReadOnlyForeignKey fk) => fk is not RuntimeForeignKey;","tryCatchPattern":null,"preventionTips":["Read FK migration-exclusion from the design-time model only.","Keep migration planning in the migrations pipeline, not runtime diagnostics."],"tags":["runtime-model","design-time-model","metadata","migrations","foreign-key"],"analyzedSha":"dbf9771522148d61a2467854921bd5dc6f6e6916","analyzedAt":"2026-08-06T20:46:03.226Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}