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 to use different collations ('{collation1}' and '{collation2}').

What it means

Two properties mapped to the same column specify different Collations (HasCollation), compared ordinally. A physical column has one collation, so EF aborts when they disagree. Thrown from ValidateCompatible via GetCollation(storeObject).

Source

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

        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(),
                    duplicateProperty.Name,
                    property.DeclaringType.DisplayName(),
                    property.Name,
                    columnName,
                    storeObject.DisplayName(),
                    previousCollation,
                    currentCollation));
        }

        var currentColumnOrder = property.GetColumnOrder(storeObject);
        var previousColumnOrder = duplicateProperty.GetColumnOrder(storeObject);
        if (currentColumnOrder != previousColumnOrder)
        {
            throw new InvalidOperationException(
                RelationalStrings.DuplicateColumnNameOrderMismatch(
                    duplicateProperty.DeclaringType.DisplayName(),

View on GitHub (pinned to dbf9771522)

Solutions

  1. Set UseCollation to the same collation name on both properties (or remove it from both).
  2. Rename one column via HasColumnName so the collations need not match.
  3. Split one entity into its own table.

Example fix

// before
modelBuilder.Entity<Student>().Property(s => s.Code).UseCollation("SQL_Latin1_General_CP1_CS_AS");
modelBuilder.Entity<Teacher>().Property(t => t.Code).UseCollation("SQL_Latin1_General_CP1_CI_AS");
// after
modelBuilder.Entity<Student>().Property(s => s.Code).UseCollation("SQL_Latin1_General_CP1_CI_AS");
modelBuilder.Entity<Teacher>().Property(t => t.Code).UseCollation("SQL_Latin1_General_CP1_CI_AS");
Defensive patterns

Strategy: validation

Validate before calling

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

Try / catch

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

Prevention

When it happens

Trigger: Sibling TPH types where one calls UseCollation("SQL_Latin1_General_CP1_CI_AS") on the property and the other uses a different collation or none on the same column; an owned entity and owner with mismatched collations; table splitting with divergent collation config.

Common situations: Adding case-sensitive comparison to one entity's code column while the sibling stays case-insensitive; cross-database porting where collation names changed on one side; using property-level UseCollation vs entity-level UseCollation inconsistently.

Related errors


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