dotnet/efcore · error · InvalidOperationException

'{entityType1}.{property1}' and '{entityType2}.{property2}'

Error message

'{entityType1}.{property1}' and '{entityType2}.{property2}' are both mapped to column '{columnName}' in '{table}', but are configured with different comments ('{comment1}' and '{comment2}').

What it means

Two properties mapped to the same column specify different database Comments (HasComment), compared ordinally. Even comments must agree on a shared column. Thrown from ValidateCompatible via GetComment(storeObject).

Source

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

        if (!currentDefaultValueSql.Equals(previousDefaultValueSql, StringComparison.OrdinalIgnoreCase))
        {
            throw new InvalidOperationException(
                RelationalStrings.DuplicateColumnNameDefaultSqlMismatch(
                    duplicateProperty.DeclaringType.DisplayName(),
                    duplicateProperty.Name,
                    property.DeclaringType.DisplayName(),
                    property.Name,
                    columnName,
                    storeObject.DisplayName(),
                    previousDefaultValueSql,
                    currentDefaultValueSql));
        }

        var currentComment = property.GetComment(storeObject) ?? "";
        var previousComment = duplicateProperty.GetComment(storeObject) ?? "";
        if (!currentComment.Equals(previousComment, StringComparison.Ordinal))
        {
            throw new InvalidOperationException(
                RelationalStrings.DuplicateColumnNameCommentMismatch(
                    duplicateProperty.DeclaringType.DisplayName(),
                    duplicateProperty.Name,
                    property.DeclaringType.DisplayName(),
                    property.Name,
                    columnName,
                    storeObject.DisplayName(),
                    previousComment,
                    currentComment));
        }

        var currentCollation = property.GetCollation(storeObject) ?? "";
        var previousCollation = duplicateProperty.GetCollation(storeObject) ?? "";
        if (!currentCollation.Equals(previousCollation, StringComparison.Ordinal))
        {
            throw new InvalidOperationException(
                RelationalStrings.DuplicateColumnNameCollationMismatch(
                    duplicateProperty.DeclaringType.DisplayName(),

View on GitHub (pinned to dbf9771522)

Solutions

  1. Set HasComment to the identical string on both properties.
  2. Remove HasComment from one side so both default to empty.
  3. If comments legitimately differ, rename one column with HasColumnName.

Example fix

// before
modelBuilder.Entity<Student>().Property(s => s.Name).HasComment("Student name");
modelBuilder.Entity<Teacher>().Property(t => t.Name).HasComment("Teacher name");
// after
modelBuilder.Entity<Student>().Property(s => s.Name).HasComment("Person name");
modelBuilder.Entity<Teacher>().Property(t => t.Name).HasComment("Person name");
Defensive patterns

Strategy: validation

Validate before calling

using (var ctx = new MyContext()) { ctx.Model.ToDebugString(); }
var a = ctx.Model.FindEntityType(typeof(Student))!.FindProperty("Name")!.GetComment() ?? "";
var b = ctx.Model.FindEntityType(typeof(Teacher))!.FindProperty("Name")!.GetComment() ?? "";
Debug.Assert(a.Equals(b, StringComparison.Ordinal), $"Comment mismatch: '{a}' vs '{b}'");

Try / catch

try { using var ctx = new MyContext(); _ = ctx.Model; }
catch (InvalidOperationException ex) when (ex.Message.Contains("different comments"))
{ log.Error("Comment mismatch on shared column: {Msg}", ex.Message); throw; }

Prevention

When it happens

Trigger: Sibling TPH types where one calls HasComment("Student name") and the other HasComment("Teacher name") on the same column; an owned entity and owner with different comments on the shared column; table splitting where each side annotates differently.

Common situations: Documentation/comment annotations added independently by different teams to mirror properties; refactoring a label on one branch only; merging models from separate assemblies where comments diverge.

Related errors


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