{"record":{"id":"6be2176efa87e195","repo":"dotnet/efcore","slug":"the-requested-configuration-is-not-stored-in-the-r-6be217","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/RelationalModelExtensions.cs","lineNumber":499,"sourceCode":"    /// <summary>\n    ///     Returns all functions contained in the model.\n    /// </summary>\n    /// <param name=\"model\">The model to get the functions in.</param>\n    public static IEnumerable<IDbFunction> GetDbFunctions(this IModel model)\n        => DbFunction.GetDbFunctions(model);\n\n    #endregion DbFunction\n\n    #region Collation\n\n    /// <summary>\n    ///     Returns the database collation.\n    /// </summary>\n    /// <param name=\"model\">The model to get the collation for.</param>\n    /// <returns>The collation.</returns>\n    public static string? GetCollation(this IReadOnlyModel model)\n        => (model is RuntimeModel)\n            ? throw new InvalidOperationException(CoreStrings.RuntimeModelMissingData)\n            : (string?)model[RelationalAnnotationNames.Collation];\n\n    /// <summary>\n    ///     Sets the database collation.\n    /// </summary>\n    /// <param name=\"model\">The model to set the collation for.</param>\n    /// <param name=\"value\">The value to set.</param>\n    public static void SetCollation(this IMutableModel model, string? value)\n        => model.SetOrRemoveAnnotation(\n            RelationalAnnotationNames.Collation,\n            Check.NullButNotEmpty(value));\n\n    /// <summary>\n    ///     Sets the database collation.\n    /// </summary>\n    /// <param name=\"model\">The model to set the collation for.</param>\n    /// <param name=\"value\">The value to set.</param>\n    /// <param name=\"fromDataAnnotation\">Indicates whether the configuration was specified using a data annotation.</param>","sourceCodeStart":481,"sourceCodeEnd":517,"githubUrl":"https://github.com/dotnet/efcore/blob/3a2006ef569de08368d59db5e1468aa8f407e4f8/src/EFCore.Relational/Extensions/RelationalModelExtensions.cs#L481-L517","documentation":"GetCollation() reads a model-level annotation that is stripped from the read-optimized runtime model. Calling it on a RuntimeModel throws to direct you to the design-time model, which retains the collation setting.","triggerScenarios":"Calling model.GetCollation() where model is a RuntimeModel instance, typically the runtime model obtained from DbContext.Model at run time rather than from IDesignTimeModel.","commonSituations":"Tooling or conventions reading collation at runtime; shared helpers bound to the runtime IModel; migration code that accidentally uses the runtime model.","solutions":["Read collation from the design-time model via DbContext.GetService<IDesignTimeModel>().Model.","Guard with 'is RuntimeModel' and branch to the design-time source.","Keep collation reads in design-time-only code."],"exampleFix":"// before\nvar collation = ctx.Model.GetCollation();\n// after\nvar collation = ctx.GetService<IDesignTimeModel>().Model.GetCollation();","handlingStrategy":"type-guard","validationCode":"IModel model = ctx.GetService<IDesignTimeModel>().Model;\nvar collation = model.GetCollation();","typeGuard":"using Microsoft.EntityFrameworkCore.Metadata.Internal;\nstatic bool IsRuntime(IModel m) => m is RuntimeModel;\nstatic string? SafeGetCollation(IModel m)\n    => m is RuntimeModel ? null : m.GetCollation();","tryCatchPattern":"try { return model.GetCollation(); }\ncatch (InvalidOperationException) when (model is RuntimeModel)\n{ return null; }","preventionTips":["Source collation reads from IDesignTimeModel.","Guard shared helpers with a RuntimeModel check.","Document that collation is a design-time-only setting."],"tags":["runtime-model","design-time","annotations","collation","model"],"backgroundTag":null,"analyzedSha":"3a2006ef569de08368d59db5e1468aa8f407e4f8","analyzedAt":"2026-08-11T23:42:04.146Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}