dotnet/efcore · error · InvalidOperationException

The requested configuration is not stored in the…

Error message

The requested configuration is not stored in the read-optimized model, please use 'DbContext.GetService<IDesignTimeModel>().Model'.

What it means

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.

Solutions

  1. Read collation from the design-time model via DbContext.GetService<IDesignTimeModel>().Model.
  2. Guard with 'is RuntimeModel' and branch to the design-time source.
  3. Keep collation reads in design-time-only code.

Example fix

// before
var collation = ctx.Model.GetCollation();
// after
var collation = ctx.GetService<IDesignTimeModel>().Model.GetCollation();
Defensive patterns

Strategy: type-guard

Validate before calling

IModel model = ctx.GetService<IDesignTimeModel>().Model;
var collation = model.GetCollation();

Type guard

using Microsoft.EntityFrameworkCore.Metadata.Internal;
static bool IsRuntime(IModel m) => m is RuntimeModel;
static string? SafeGetCollation(IModel m)
    => m is RuntimeModel ? null : m.GetCollation();

Try / catch

try { return model.GetCollation(); }
catch (InvalidOperationException) when (model is RuntimeModel)
{ return null; }

Prevention

When it happens

Trigger: Calling model.GetCollation() where model is a RuntimeModel instance, typically the runtime model obtained from DbContext.Model at run time rather than from IDesignTimeModel.

Common situations: Tooling or conventions reading collation at runtime; shared helpers bound to the runtime IModel; migration code that accidentally uses the runtime model.

Related errors


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

Appendix: 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 3a2006ef56)