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

  1. Read collation from the design-time model: dbContext.GetService<IDesignTimeModel>().Model.GetCollation().
  2. 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

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


AI-assisted analysis of dotnet/efcore@dbf9771522 (2026-08-06). Data as JSON: /api/errors/6be2176efa87e195. Report an issue: GitHub.