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
IReadOnlyModel.GetCollation() throws CoreStrings.RuntimeModelMissingData on a RuntimeModel, because the database collation annotation is design-time only (RelationalModelExtensions.cs:499). The runtime model is optimized and omits collation metadata.
Source
Thrown at src/EFCore.Relational/Extensions/RelationalModelExtensions.cs:499
/// <summary>
/// Returns all functions contained in the model.
/// </summary>
/// <param name="model">The model to get the functions in.</param>
public static IEnumerable<IDbFunction> GetDbFunctions(this IModel model)
=> DbFunction.GetDbFunctions(model);
#endregion DbFunction
#region Collation
/// <summary>
/// Returns the database collation.
/// </summary>
/// <param name="model">The model to get the collation for.</param>
/// <returns>The collation.</returns>
public static string? GetCollation(this IReadOnlyModel model)
=> (model is RuntimeModel)
? throw new InvalidOperationException(CoreStrings.RuntimeModelMissingData)
: (string?)model[RelationalAnnotationNames.Collation];
/// <summary>
/// Sets the database collation.
/// </summary>
/// <param name="model">The model to set the collation for.</param>
/// <param name="value">The value to set.</param>
public static void SetCollation(this IMutableModel model, string? value)
=> model.SetOrRemoveAnnotation(
RelationalAnnotationNames.Collation,
Check.NullButNotEmpty(value));
/// <summary>
/// Sets the database collation.
/// </summary>
/// <param name="model">The model to set the collation for.</param>
/// <param name="value">The value to set.</param>
/// <param name="fromDataAnnotation">Indicates whether the configuration was specified using a data annotation.</param>View on GitHub (pinned to dbf9771522)
Solutions
- Read collation from the design-time model: dbContext.GetService<IDesignTimeModel>().Model.GetCollation().
- Keep collation-related introspection in migrations/scaffolding code that operates on the design-time model.
Example fix
// before var coll = dbContext.Model.GetCollation(); // throws // after var coll = dbContext.GetService<IDesignTimeModel>().Model.GetCollation();
Defensive patterns
Strategy: validation
Validate before calling
var coll = dbContext.GetService<IDesignTimeModel>().Model.GetCollation();
Type guard
static bool IsDesignTime(IModel m) => m is not RuntimeModel;
Prevention
- Route collation reads through IDesignTimeModel.
- Do not expose collation metadata in runtime app code.
When it happens
Trigger: Calling dbContext.Model.GetCollation() (runtime model) instead of the design-time model's GetCollation().
Common situations: Diagnostic or logging code that reports the configured collation at runtime; sharing metadata-reading code across runtime and design-time paths.
Related errors
- The requested configuration is not stored in the read-optimi
- The requested configuration is not stored in the read-optimi
- The requested configuration is not stored in the read-optimi
- The requested configuration is not stored in the read-optimi
- 'Except' cannot be called on the builder returned by 'HasAut
AI-assisted analysis of dotnet/efcore@dbf9771522 (2026-08-06).
Data as JSON: /api/errors/6be2176efa87e195.
Report an issue: GitHub.