dotnet/efcore · error · InvalidOperationException

The table ' ' cannot be used for entity type ' ' since it…

Error message

The table '{table}' cannot be used for entity type '{entityType}' since it is being used for entity type '{otherEntityType}' and there is a relationship between their primary keys in which '{entityType}' is the dependent, but '{entityType}' has a base entity type mapped to a different table. Either map '{otherEntityType}' to a different table, or invert the relationship between '{entityType}' and '{otherEntityType}'.

What it means

Thrown by ValidateSharedTableCompatibility when multiple entity types share a table, one of them (mappedType) has a foreign key on its primary key pointing to another sharing type (a linking FK), and mappedType has a BaseType — i.e. it is in an inheritance hierarchy mapped to a different table. Joining a derived entity to a sibling via PK-FK while splitting its hierarchy across tables is contradictory and rejected.

Solutions

  1. Map one of the two types to its own table so the derived relationship is no longer split: builder.ToTable("B").
  2. Invert the relationship so the dependent becomes the principal of the linking FK and the base-type constraint is satisfied.
  3. Resolve the inheritance conflict: either flatten the hierarchy for these types or remap the base type to the same table.

Example fix

// before
// class Derived : Base, where Base -> "BaseTable", Derived shares "SharedTable" with Principal
[Table("SharedTable")] public class Derived : Base { ... }
builder.Entity<Derived>().HasOne(d => d.Principal).WithOne().HasForeignKey<Derived>(d => d.Id);

// after
builder.Entity<Derived>().ToTable("DerivedTable"); // break the share
Defensive patterns

Strategy: validation

Validate before calling

foreach (var et in model.GetEntityTypes())
{
    if (et.BaseType == null) continue;
    var pk = et.FindPrimaryKey();
    if (pk == null) continue;
    foreach (var fk in et.FindForeignKeys(pk.Properties))
    {
        if (fk.PrincipalKey.IsPrimaryKey()
            && !fk.PrincipalEntityType.IsAssignableFrom(et)
            && !string.Equals(et.GetTableName(), fk.PrincipalEntityType.GetTableName(), StringComparison.Ordinal))
        {
            Console.WriteLine($"{et.Name} derives from {et.BaseType.Name} (different table) but has PK FK to {fk.PrincipalEntityType.Name} on the same table.");
        }
    }
}

Prevention

When it happens

Trigger: Entity types A and B share a table; B's primary key has an FK to A's primary key; B also derives from a base entity mapped to another table. The validator detects mappedType.BaseType != null alongside the linking FK and throws.

Common situations: TPT or TPC inheritance combined with table sharing / many-to-many link tables; refactoring inheritance while leaving an old FK-based table-share in place.

Related errors


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

Appendix: source

Thrown at src/EFCore.Relational/Infrastructure/RelationalModelValidator.cs:1174

        var unvalidatedTypes = new HashSet<IEntityType>(mappedTypes);
        IEntityType? root = null;
        foreach (var mappedType in mappedTypes)
        {
            if (mappedType.BaseType != null && unvalidatedTypes.Contains(mappedType.BaseType))
            {
                continue;
            }

            var primaryKey = mappedType.FindPrimaryKey();
            if (primaryKey != null
                && (mappedType.FindForeignKeys(primaryKey.Properties)
                    .FirstOrDefault(fk => fk.PrincipalKey.IsPrimaryKey()
                        && !fk.PrincipalEntityType.IsAssignableFrom(fk.DeclaringEntityType)
                        && unvalidatedTypes.Contains(fk.PrincipalEntityType)) is { } linkingFK))
            {
                if (mappedType.BaseType != null)
                {
                    throw new InvalidOperationException(
                        RelationalStrings.IncompatibleTableDerivedRelationship(
                            table.DisplayName(),
                            mappedType.DisplayName(),
                            linkingFK.PrincipalEntityType.DisplayName()));
                }

                continue;
            }

            if (root != null)
            {
                throw new InvalidOperationException(
                    RelationalStrings.IncompatibleTableNoRelationship(
                        table.DisplayName(),
                        mappedType.DisplayName(),
                        root.DisplayName()));
            }

View on GitHub (pinned to 3a2006ef56)