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 default values ('{value1}' and '{value2}').

What it means

Two properties mapped to the same column have DefaultValue facets that, after value-converter normalization, produce different default values. The physical column can have only one DEFAULT, so model finalization aborts. Thrown from ValidateCompatible (the TryGetDefaultValue/GetDefaultColumnValue branch).

Source

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

                    property.Name,
                    columnName,
                    storeObject.DisplayName(),
                    previousStored,
                    currentStored));
        }

        var hasDefaultValue = property.TryGetDefaultValue(storeObject, out var currentDefaultValue);
        var duplicateHasDefaultValue = duplicateProperty.TryGetDefaultValue(storeObject, out var previousDefaultValue);
        if ((hasDefaultValue
                || duplicateHasDefaultValue)
            && !Equals(currentDefaultValue, previousDefaultValue))
        {
            currentDefaultValue = GetDefaultColumnValue(property, storeObject);
            previousDefaultValue = GetDefaultColumnValue(duplicateProperty, storeObject);

            if (!Equals(currentDefaultValue, previousDefaultValue))
            {
                throw new InvalidOperationException(
                    RelationalStrings.DuplicateColumnNameDefaultSqlMismatch(
                        duplicateProperty.DeclaringType.DisplayName(),
                        duplicateProperty.Name,
                        property.DeclaringType.DisplayName(),
                        property.Name,
                        columnName,
                        storeObject.DisplayName(),
                        previousDefaultValue ?? "NULL",
                        currentDefaultValue ?? "NULL"));
            }
        }

        var currentDefaultValueSql = property.GetDefaultValueSql(storeObject) ?? "";
        var previousDefaultValueSql = duplicateProperty.GetDefaultValueSql(storeObject) ?? "";
        if (!currentDefaultValueSql.Equals(previousDefaultValueSql, StringComparison.OrdinalIgnoreCase))
        {
            throw new InvalidOperationException(
                RelationalStrings.DuplicateColumnNameDefaultSqlMismatch(

View on GitHub (pinned to dbf9771522)

Solutions

  1. Set HasDefaultValue to the same value on both properties (after value-converter normalization).
  2. If different defaults are needed, rename one column with HasColumnName.
  3. Drop the explicit default from one side and let it match the other.

Example fix

// before
modelBuilder.Entity<Student>().Property(s => s.Status).HasDefaultValue(0);
modelBuilder.Entity<Teacher>().Property(t => t.Status).HasDefaultValue(1);
// after
const int DefaultStatus = 0;
modelBuilder.Entity<Student>().Property(s => s.Status).HasDefaultValue(DefaultStatus);
modelBuilder.Entity<Teacher>().Property(t => t.Status).HasDefaultValue(DefaultStatus);
Defensive patterns

Strategy: validation

Validate before calling

using (var ctx = new MyContext()) { ctx.Model.ToDebugString(); }
ctx.Model.FindEntityType(typeof(Student))!.FindProperty("Status")!.TryGetDefaultValue(out var a);
ctx.Model.FindEntityType(typeof(Teacher))!.FindProperty("Status")!.TryGetDefaultValue(out var b);
Debug.Assert(Equals(a, b), $"DefaultValue mismatch: {a} vs {b}");

Try / catch

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

Prevention

When it happens

Trigger: Sibling TPH types where one calls HasDefaultValue(1) and the other HasDefaultValue(0) on a same-named property; an owned entity and owner setting different default values; table splitting with conflicting defaults; one side using a value converter that changes the converted default.

Common situations: Changing a Status default from 0 to 1 on one entity only; refactoring a value converter so the converted default no longer matches the sibling; introducing a default for a new property that collides with an existing column's default.

Related errors


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