dotnet/efcore · error · InvalidOperationException

The type ' ' is not mapped to the store object ' '.

Error message

The type '{entityType}' is not mapped to the store object '{table}'.

What it means

The store-object overload of IsTableExcludedFromMigrations first checks for a mapping fragment, then falls back to verifying that StoreObjectIdentifier.Create(entityType, storeObjectType) equals the supplied storeObject. If neither holds, the entity is not mapped to that table, and the method throws TableNotMappedEntityType rather than returning a misleading value.

Solutions

  1. Verify the entity is actually mapped to the store object with StoreObjectIdentifier.Create(entityType, type) before calling.
  2. Use the correct entity type for the store object in question.
  3. If handling hierarchies, walk BaseType/derived types to find the mapped type.
  4. Return early when FindMappingFragment(storeObject) is null and the create check fails.

Example fix

// before
var so = StoreObjectIdentifier.Create(otherEntity, StoreObjectType.Table)!.Value;
bool x = entity.IsTableExcludedFromMigrations(so);
// after
if (StoreObjectIdentifier.Create(entity, StoreObjectType.Table) is { } so)
    bool x = entity.IsTableExcludedFromMigrations(so);
Defensive patterns

Strategy: validation

Validate before calling

// Confirm the entity maps to the store object before calling.
if (StoreObjectIdentifier.Create(entityType, storeObject.StoreObjectType) != storeObject)
{
    // entity is not mapped to this store object; do not call IsTableExcludedFromMigrations(storeObject).
    return;
}

Type guard

static bool IsMappedToStore(IEntityType et, in StoreObjectIdentifier so)
    => StoreObjectIdentifier.Create(et, so.StoreObjectType) == so;

Try / catch

try { return entityType.IsTableExcludedFromMigrations(storeObject); }
catch (InvalidOperationException ex) when (ex.Message.Contains("is not mapped to the store object"))
{ return false; }

Prevention

When it happens

Trigger: Calling entityType.IsTableExcludedFromMigrations(storeObject) where the entity has no mapping fragment for that storeObject AND StoreObjectIdentifier.Create does not reproduce the same storeObject — i.e. the entity is genuinely not mapped to that table/view/sproc.

Common situations: Passing a StoreObjectIdentifier that belongs to a different entity; querying an override for a table the entity type does not map to; TPT/TPC hierarchies where the store object belongs to a sibling type.

Related errors


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

Appendix: source

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

    /// </summary>
    /// <param name="entityType">The entity type.</param>
    /// <param name="storeObject">The identifier of the table-like store object.</param>
    /// <returns>A value indicating whether the associated table is ignored by Migrations.</returns>
    public static bool IsTableExcludedFromMigrations(
        this IReadOnlyEntityType entityType,
        in StoreObjectIdentifier storeObject)
    {
        if (entityType is RuntimeEntityType)
        {
            throw new InvalidOperationException(CoreStrings.RuntimeModelMissingData);
        }

        var overrides = entityType.FindMappingFragment(storeObject);
        return overrides != null
            ? overrides.IsTableExcludedFromMigrations ?? entityType.IsTableExcludedFromMigrations()
            : StoreObjectIdentifier.Create(entityType, storeObject.StoreObjectType) == storeObject
                ? entityType.IsTableExcludedFromMigrations()
                : throw new InvalidOperationException(
                    RelationalStrings.TableNotMappedEntityType(entityType.DisplayName(), storeObject.DisplayName()));
    }

    /// <summary>
    ///     Sets a value indicating whether the associated table is ignored by Migrations.
    /// </summary>
    /// <param name="entityType">The entity type.</param>
    /// <param name="excluded">A value indicating whether the associated table is ignored by Migrations.</param>
    public static void SetIsTableExcludedFromMigrations(this IMutableEntityType entityType, bool? excluded)
        => entityType.SetOrRemoveAnnotation(RelationalAnnotationNames.IsTableExcludedFromMigrations, excluded);

    /// <summary>
    ///     Sets a value indicating whether the associated table is ignored by Migrations.
    /// </summary>
    /// <param name="entityType">The entity type.</param>
    /// <param name="excluded">A value indicating whether the associated table is ignored by Migrations.</param>
    /// <param name="fromDataAnnotation">Indicates whether the configuration was specified using a data annotation.</param>
    /// <returns>The configured value.</returns>

View on GitHub (pinned to 3a2006ef56)