dotnet/efcore · error · InvalidOperationException

' . ' and ' . ' are both mapped to column ' ' in ' ', but…

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 sharing a column declare different resolved default VALUES (HasDefaultValue). The validator first checks whether either side has a default and, if so, resolves both to effective values (including CLR defaults via GetDefaultColumnValue) and compares them. A mismatch would generate contradictory DEFAULT constraints in migrations, so EF rejects the model.

Solutions

  1. Call HasDefaultValue(<same-value>) on both properties.
  2. Drop HasDefaultValue from one side so both resolve to the same effective value.
  3. Use HasDefaultValueSql on both instead of HasDefaultValue, or remap one property to its own column.

Example fix

// before
modelBuilder.Entity<User>().Property(u => u.IsActive).HasDefaultValue(true);
modelBuilder.Entity<UserSecrets>().Property(s => s.IsActive).HasDefaultValue(false); // shared column

// after
modelBuilder.Entity<User>().Property(u => u.IsActive).HasDefaultValue(true);
modelBuilder.Entity<UserSecrets>().Property(s => s.IsActive).HasDefaultValue(true);
Defensive patterns

Strategy: validation

Validate before calling

foreach (var (col, props) in sharedColumnGroups)
{
    var resolved = props.Select(p => p.TryGetDefaultValue(storeObject, out var v) ? v : null).ToList();
    if (resolved.Any(r => r is not null) && resolved.Distinct().Count() > 1)
        Console.WriteLine($"Column {col} has conflicting default values");
}

Try / catch

try { _ = db.Model; } catch (InvalidOperationException ex) when (ex.Message.Contains("different default values") && ex.Message.Contains("NULL") == false) { // align HasDefaultValue on both properties }

Prevention

When it happens

Trigger: ValidateCompatible() calls TryGetDefaultValue on both properties; if either returns true and the resolved values (after GetDefaultColumnValue fallback) are not Equal, it throws. Thrown from the DuplicateColumnNameDefaultSqlMismatch resource with the resolved values.

Common situations: Owned entity and owner both set HasDefaultValue(0) vs HasDefaultValue(1) on a shared Status column; one side relies on the CLR default while the other sets an explicit default; refactor that split a column but kept inconsistent defaults.

Related errors


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

Appendix: 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 3a2006ef56)