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 the comment '{comment}' does not match the comment '{otherComment}'.

What it means

Thrown when entity types sharing a table have different table-level comments (GetComment()) assigned. The validator collects the comment from the first entity, then for each connected entity compares its comment with the running value; mismatched non-null comments are rejected because they would produce contradictory ALTER TABLE ... COMMENT DDL.

Solutions

  1. Set the same comment on every entity type sharing the table.
  2. Set the comment only on one entity (the others leave it null) and let the validator propagate it.
  3. Remove the conflicting HasComment call.

Example fix

// before
builder.Entity<Customer>().ToTable("Users", t => t.HasComment("Customers"));
builder.Entity<User>().ToTable("Users", t => t.HasComment("Users"));

// after
builder.Entity<Customer>().ToTable("Users", t => t.HasComment("Application users"));
builder.Entity<User>().ToTable("Users"); // comment inherited
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 comments = grp.Select(e => e.GetComment()).Where(c => c != null).Distinct().ToList();
    if (comments.Count > 1)
        Console.WriteLine($"Table '{grp.Key}' has conflicting comments: {string.Join(" | ", comments)}");
}

Prevention

When it happens

Trigger: builder.Entity<A>().ToTable("X", t => t.HasComment("A comment")) and builder.Entity<B>().ToTable("X", t => t.HasComment("B comment")) with A and B sharing the table. The validator sees comment != null and nextComment != null and they are not equal.

Common situations: Adding a comment to one of several shared entities; merging owned entities where each sets a comment; refactoring an entity to owned without removing the old comment config.

Related errors


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

Appendix: source

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

                        throw new InvalidOperationException(
                            RelationalStrings.IncompatibleTableKeyNameMismatch(
                                table.DisplayName(),
                                entityType.DisplayName(),
                                nextEntityType.DisplayName(),
                                key.GetName(table),
                                key.Properties.Format(),
                                otherKey.GetName(table),
                                otherKey.Properties.Format()));
                    }
                }

                var nextComment = nextEntityType.GetComment();
                if (comment != null)
                {
                    if (nextComment != null
                        && !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(),

View on GitHub (pinned to 3a2006ef56)