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 column orders ('{columnOrder1}' and '{columnOrder2}').

What it means

Two properties mapped to the same column specify different ColumnOrder values via HasColumnOrder(n). The validator needs the column to occupy one ordinal position, so EF aborts. Thrown from ValidateCompatible via GetColumnOrder(storeObject).

Source

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

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

    /// <summary>
    ///     Returns the object that is used as the default value for the column the property is mapped to.
    /// </summary>
    /// <param name="property">The property to get the default value for.</param>
    /// <param name="storeObject">The identifier of the store object.</param>
    /// <returns>The object that is used as the default value for the column the property is mapped to.</returns>

View on GitHub (pinned to dbf9771522)

Solutions

  1. Set HasColumnOrder to the same integer on both properties sharing the column.
  2. Remove HasColumnOrder from one side so both default to null (EF picks ordering).
  3. Rename one column with HasColumnName if the orderings legitimately differ.

Example fix

// before
modelBuilder.Entity<Student>().Property(s => s.Name).HasColumnOrder(3);
modelBuilder.Entity<Teacher>().Property(t => t.Name).HasColumnOrder(5);
// after
modelBuilder.Entity<Student>().Property(s => s.Name).HasColumnOrder(3);
modelBuilder.Entity<Teacher>().Property(t => t.Name).HasColumnOrder(3);
Defensive patterns

Strategy: validation

Validate before calling

using (var ctx = new MyContext()) { ctx.Model.ToDebugString(); }
var a = ctx.Model.FindEntityType(typeof(Student))!.FindProperty("Name")!.GetColumnOrder();
var b = ctx.Model.FindEntityType(typeof(Teacher))!.FindProperty("Name")!.GetColumnOrder();
Debug.Assert(a == b, $"ColumnOrder mismatch: {a} vs {b}");

Try / catch

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

Prevention

When it happens

Trigger: Sibling TPH types where one calls HasColumnOrder(3) and the other HasColumnOrder(5) on the same column; an owned entity and owner with conflicting order values; table splitting where each side sets an explicit order for a shared column.

Common situations: Adding HasColumnOrder on one entity to control migration column ordering while forgetting the sibling that shares the column; merging configurations from teams that picked different ordinals; refactoring order numbers on one branch only.

Related errors


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