dotnet/efcore · error · InvalidOperationException

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

Error message

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

What it means

Thrown by TableBase.IsOptional when OptionalTypes has been populated (the table is shared/has optional mappings) but the queried typeBase is not present in it. It means the type is not mapped to this table among the optional-mapping set, so its optionality is undefined — EF refuses to silently default it.

Source

Thrown at src/EFCore.Relational/Metadata/Internal/TableBase.cs:234

        {
            referencingForeignKeys = new SortedSet<IForeignKey>(ForeignKeyComparer.Instance);
            ReferencingRowInternalForeignKeys[principalEntityType] = referencingForeignKeys;
        }

        ((SortedSet<IForeignKey>)referencingForeignKeys).Add(foreignKey);
    }

    /// <inheritdoc />
    public virtual bool IsOptional(ITypeBase typeBase)
    {
        if (OptionalTypes == null)
        {
            CheckMappedType(typeBase);
            return false;
        }

        return !OptionalTypes.TryGetValue(typeBase, out var optional)
            ? throw new InvalidOperationException(
                RelationalStrings.TableNotMappedEntityType(typeBase.DisplayName(), ((ITableBase)this).SchemaQualifiedName))
            : optional;
    }

    private void CheckMappedType(ITypeBase typeBase)
    {
        if (EntityTypeMappings.All(m => m.TypeBase != typeBase)
            && ComplexTypeMappings.All(m => m.TypeBase != typeBase))
        {
            throw new InvalidOperationException(
                RelationalStrings.TableNotMappedEntityType(typeBase.DisplayName(), ((ITableBase)this).SchemaQualifiedName));
        }
    }

    /// <summary>
    ///     This is an internal API that supports the Entity Framework Core infrastructure and not subject to
    ///     the same compatibility standards as public APIs. It may be changed or removed without notice in
    ///     any release. You should only use it directly in your code with extreme caution and knowing that

View on GitHub (pinned to dbf9771522)

Solutions

  1. Ensure the entity type is actually mapped to the table (ToTable / owned mapping / inheritance strategy).
  2. Confirm the type is the same instance used in the table mapping (not a different IEntityType from another model).
  3. If the type should not be on this table, redirect the call to the correct ITableBase for that type.

Example fix

// before - OrderDetail expected to share Orders table but not mapped
var optional = table.IsOptional(orderDetailType); // throws

// after - ensure mapping before querying
modelBuilder.Entity<Order>()
    .OwnsOne(o => o.Detail, d => d.ToTable("Orders"));
var optional = table.IsOptional(orderDetailType);
Defensive patterns

Strategy: validation

Validate before calling

static bool IsMappedToTable(ITableBase table, ITypeBase type)
    => table.EntityTypeMappings.Any(m => m.TypeBase == type)
        || table.ComplexTypeMappings.Any(m => m.TypeBase == type);

// before calling table.IsOptional(type):
if (!IsMappedToTable(table, type))
    throw new InvalidOperationException($"{type.Name} is not mapped to {table.SchemaQualifiedName}.");

Prevention

When it happens

Trigger: TableBase.cs:233-236: OptionalTypes != null and !OptionalTypes.TryGetValue(typeBase, out _). Reached via ITableBase.IsOptional(typeBase) during relational model finalization when a type that is not part of the table's optional mapping set is queried.

Common situations: An entity type that was expected to share a table but is not actually mapped to it (wrong ToTable, missing ownership, TPT misconfiguration); querying IsOptional for a type removed from the model; a derived type not participating in the shared table.

Related errors


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