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

GetComment() reads the RelationalComment annotation from an entity type. The read-optimized runtime model (RuntimeEntityType) intentionally strips design-time-only configuration such as table comments. The method explicitly checks for RuntimeEntityType and throws to redirect you to the design-time model, which retains these annotations.

Solutions

  1. Obtain the design-time model via DbContext.GetService<IDesignTimeModel>().Model and call GetComment on its entity types.
  2. Keep design-time-only annotation reads out of runtime code paths.
  3. Add an is RuntimeEntityType guard at the call site to choose the correct model source.

Example fix

// before
var comment = ctx.Model.GetEntityTypes().First().GetComment();
// after
var dtm = ctx.GetService<IDesignTimeModel>().Model;
var comment = dtm.GetEntityTypes().First().GetComment();
Defensive patterns

Strategy: type-guard

Validate before calling

// Use the design-time model whenever you need table comments.
IModel model = ctx.GetService<IDesignTimeModel>().Model;
var comment = entityTypeFromDesignTimeModel.GetComment();

Type guard

using Microsoft.EntityFrameworkCore.Metadata.Internal;
static bool IsRuntime(IEntityType et) => et is RuntimeEntityType;
static string? SafeGetComment(IEntityType et)
    => et is RuntimeEntityType ? null : et.GetComment();

Try / catch

try { return entityType.GetComment(); }
catch (InvalidOperationException) when (entityType is RuntimeEntityType)
{ /* fall back to IDesignTimeModel */ }

Prevention

When it happens

Trigger: Calling entityType.GetComment() where entityType is a RuntimeEntityType instance — typically obtained from the runtime IModel (DbContext.Model at runtime) rather than the design-time model.

Common situations: Custom migrations or scaffolding code that iterates DbContext.Model (runtime) instead of IDesignTimeModel; conventions or tools reading comments at runtime; shared code paths that receive either model kind.

Related errors


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

Appendix: source

Thrown at src/EFCore.Relational/Extensions/RelationalEntityTypeExtensions.cs:966

    ///     It is useful when iterating over all entity types to avoid processing the same check constraint more than once.
    ///     Use <see cref="GetCheckConstraints(IEntityType)" /> to also return check constraints declared on base types.
    /// </remarks>
    /// <param name="entityType">The entity type to get the check constraints for.</param>
    public static IEnumerable<ICheckConstraint> GetDeclaredCheckConstraints(this IEntityType entityType)
        => CheckConstraint.GetDeclaredCheckConstraints(entityType).Cast<ICheckConstraint>();

    #endregion Check constraint

    #region Comment

    /// <summary>
    ///     Returns the comment for the table this entity is mapped to.
    /// </summary>
    /// <param name="entityType">The entity type.</param>
    /// <returns>The comment for the table this entity is mapped to.</returns>
    public static string? GetComment(this IReadOnlyEntityType entityType)
        => (entityType is RuntimeEntityType)
            ? throw new InvalidOperationException(CoreStrings.RuntimeModelMissingData)
            : (string?)entityType[RelationalAnnotationNames.Comment];

    /// <summary>
    ///     Configures a comment to be applied to the table this entity is mapped to.
    /// </summary>
    /// <param name="entityType">The entity type.</param>
    /// <param name="comment">The comment for the table this entity is mapped to.</param>
    public static void SetComment(this IMutableEntityType entityType, string? comment)
        => entityType.SetOrRemoveAnnotation(RelationalAnnotationNames.Comment, comment);

    /// <summary>
    ///     Configures a comment to be applied to the table this entity is mapped to.
    /// </summary>
    /// <param name="entityType">The entity type.</param>
    /// <param name="comment">The comment for the table.</param>
    /// <param name="fromDataAnnotation">Indicates whether the configuration was specified using a data annotation.</param>
    /// <returns>The configured comment.</returns>
    public static string? SetComment(

View on GitHub (pinned to 3a2006ef56)