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 with different maximum lengths ('{maxLength1}' and '{maxLength2}').

What it means

Two properties mapped to the same column have different configured maximum lengths. Because they share a physical column, EF cannot pick one max length over the other and refuses to silently truncate, so it throws. Length drives the store type (e.g. nvarchar(N)), so a mismatch would create an ambiguous schema.

Solutions

  1. Set the same MaxLength / string length on both properties (via HasMaxLength, [StringLength] or [MaxLength]).
  2. Explicitly set one to match the other using the fluent API in OnModelCreating.
  3. Remove the explicit length on one side so it inherits the convention default and matches.
  4. If they are truly different columns, remap one to a distinct column name.

Example fix

// before
modelBuilder.Entity<User>().Property(u => u.Email).HasMaxLength(100);
modelBuilder.Entity<UserProfile>().Property(p => p.Email).HasMaxLength(200); // shared column 'Email'

// after
modelBuilder.Entity<User>().Property(u => u.Email).HasMaxLength(200);
modelBuilder.Entity<UserProfile>().Property(p => p.Email).HasMaxLength(200);
Defensive patterns

Strategy: validation

Validate before calling

// Compare max length on every pair of properties sharing a column.
foreach (var (col, props) in sharedColumnGroups)
{
    var lengths = props.Select(p => p.GetMaxLength(storeObject)).Distinct().ToList();
    if (lengths.Count > 1)
        Console.WriteLine($"Column {col} has conflicting max lengths: {string.Join(", ", lengths)}");
}

Try / catch

try { _ = db.Model; } catch (InvalidOperationException ex) when (ex.Message.Contains("different maximum lengths")) { // align HasMaxLength on the conflicting properties }

Prevention

When it happens

Trigger: ValidateCompatible() compares property.GetMaxLength(storeObject) vs duplicateProperty.GetMaxLength(storeObject) on a duplicate column and they differ (e.g. 50 vs 100). Common with table splitting or TPT where both types own a column of the same name.

Common situations: Owned entity and owner both expose a 'Name' string on the shared table with different MaxLength attributes; TPH where derived types restate the length; data annotations vs fluent API disagreement.

Related errors


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

Appendix: source

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

    /// <param name="property">A property.</param>
    /// <param name="duplicateProperty">Another property.</param>
    /// <param name="columnName">The column name.</param>
    /// <param name="storeObject">The identifier of the store object.</param>
    /// <param name="logger">The logger to use.</param>
    protected virtual void ValidateCompatible(
        IProperty property,
        IProperty duplicateProperty,
        string columnName,
        in StoreObjectIdentifier storeObject,
        IDiagnosticsLogger<DbLoggerCategory.Model.Validation> logger)
    {
        // NB: Properties can have different nullability, the resulting column will be non-nullable if any of the properties is non-nullable

        var currentMaxLength = property.GetMaxLength(storeObject);
        var previousMaxLength = duplicateProperty.GetMaxLength(storeObject);
        if (currentMaxLength != previousMaxLength)
        {
            throw new InvalidOperationException(
                RelationalStrings.DuplicateColumnNameMaxLengthMismatch(
                    duplicateProperty.DeclaringType.DisplayName(),
                    duplicateProperty.Name,
                    property.DeclaringType.DisplayName(),
                    property.Name,
                    columnName,
                    storeObject.DisplayName(),
                    previousMaxLength,
                    currentMaxLength));
        }

        if (property.IsUnicode(storeObject) != duplicateProperty.IsUnicode(storeObject))
        {
            throw new InvalidOperationException(
                RelationalStrings.DuplicateColumnNameUnicodenessMismatch(
                    duplicateProperty.DeclaringType.DisplayName(),
                    duplicateProperty.Name,
                    property.DeclaringType.DisplayName(),

View on GitHub (pinned to 3a2006ef56)