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 is excluded from migrations on one entity type but not on the other. Exclude the table from migrations on all entity types mapped to the table.

What it means

Thrown when entity types sharing a table disagree on whether the table is excluded from migrations (IsTableExcludedFromMigrations). The validator stores isExcluded for the first type and compares !nextEntityType.IsTableExcludedFromMigrations(table) with that flag; if one entity opts out of migrations for the shared table and another does not, the configuration is contradictory and the migration generator would emit conflicting behavior.

Solutions

  1. Apply the same exclusion choice to every entity type sharing the table: all excluded, or none excluded.
  2. Use builder.Entity<A>().ToTable("X", t => t.ExcludeFromMigrations()) and replicate on every sharing entity, or remove the call everywhere.
  3. For owned entities, exclude from the owner so they cascade — verify with ToView() vs ToTable() choices if the table should be read-only.

Example fix

// before
builder.Entity<Customer>().ToTable("Users").ExcludeFromMigrations();
builder.Entity<User>().ToTable("Users");

// after
builder.Entity<Customer>().ToTable("Users", t => t.ExcludeFromMigrations());
builder.Entity<User>().ToTable("Users", t => t.ExcludeFromMigrations());
Defensive patterns

Strategy: validation

Validate before calling

var byTable = model.GetEntityTypes().Where(e => e.GetTableName() != null).GroupBy(e => e.GetTableName());
foreach (var grp in byTable)
{
    var flags = grp.Select(e => e.IsTableExcludedFromMigrations(StoreObjectIdentifier.Table(grp.Key))).Distinct().ToList();
    if (flags.Count > 1)
        Console.WriteLine($"Table '{grp.Key}' has inconsistent ExcludeFromMigrations across entity types.");
}

Prevention

When it happens

Trigger: builder.Entity<A>().ToTable("X").ExcludeFromMigrations() while builder.Entity<B>().ToTable("X") does not exclude. Common when an owned entity is excluded but its owner is not, or when one half of a 1:1 table share is excluded by mistake.

Common situations: Marking only the principal or only the dependent as excluded; multi-tenant pre-provisioned tables where one entity type is excluded; refactoring migrations exclusion logic incompletely.

Related errors


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

Appendix: source

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

                        && !comment.Equals(nextComment, StringComparison.Ordinal))
                    {
                        throw new InvalidOperationException(
                            RelationalStrings.IncompatibleTableCommentMismatch(
                                table.DisplayName(),
                                entityType.DisplayName(),
                                nextEntityType.DisplayName(),
                                comment,
                                nextComment));
                    }
                }
                else
                {
                    comment = nextComment;
                }

                if (isExcluded.Equals(!nextEntityType.IsTableExcludedFromMigrations(table)))
                {
                    throw new InvalidOperationException(
                        RelationalStrings.IncompatibleTableExcludedMismatch(
                            table.DisplayName(),
                            entityType.DisplayName(),
                            nextEntityType.DisplayName()));
                }

                typesToValidate.Enqueue(nextEntityType);
            }

            foreach (var typeToValidate in typesToValidate.Skip(typesToValidateLeft))
            {
                unvalidatedTypes.Remove(typeToValidate);
            }
        }

        if (unvalidatedTypes.Count == 0)
        {
            return;

View on GitHub (pinned to 3a2006ef56)