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 have different unicode configurations.

What it means

Two properties mapped to the same physical column disagree on the Unicode (nvarchar vs varchar) facet. EF Core requires shared columns to agree because the underlying database column can only be one type. Thrown from ValidateCompatible during the shared-table compatibility check.

Source

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

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

        if (property.IsFixedLength(storeObject) != duplicateProperty.IsFixedLength(storeObject))
        {
            throw new InvalidOperationException(
                RelationalStrings.DuplicateColumnNameFixedLengthMismatch(
                    duplicateProperty.DeclaringType.DisplayName(),
                    duplicateProperty.Name,
                    property.DeclaringType.DisplayName(),
                    property.Name,
                    columnName,

View on GitHub (pinned to dbf9771522)

Solutions

  1. Set IsUnicode consistently (typically true) on both properties mapping to the shared column.
  2. Use HasColumnName to split the conflicting property onto its own column if unicode really should differ.
  3. Remove the explicit IsUnicode from one side so both default identically for the CLR type.
  4. Move one entity to its own table with ToTable to avoid sharing.

Example fix

// before
modelBuilder.Entity<Student>().Property(s => s.Code).IsUnicode(false);
modelBuilder.Entity<Teacher>().Property(t => t.Code).IsUnicode(true);
// after
modelBuilder.Entity<Student>().Property(s => s.Code).IsUnicode(false);
modelBuilder.Entity<Teacher>().Property(t => t.Code).IsUnicode(false);
Defensive patterns

Strategy: validation

Validate before calling

using (var ctx = new MyContext()) { ctx.Model.ToDebugString(); }
// Assert unicode parity in a test
var a = ctx.Model.FindEntityType(typeof(Student))!.FindProperty("Code")!.IsUnicode();
var b = ctx.Model.FindEntityType(typeof(Teacher))!.FindProperty("Code")!.IsUnicode();
Debug.Assert(a == b, $"Unicode mismatch: {a} vs {b}");

Try / catch

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

Prevention

When it happens

Trigger: Sibling derived types in a TPH hierarchy where one calls IsUnicode(true) and the other IsUnicode(false) for a same-named property; an owned type configured IsUnicode while the owner's matching column is not; two entities split to one table with mismatched unicode annotations.

Common situations: A property added by a developer who set IsUnicode(false) for ASCII storage while an existing sibling left the default (true on most providers); mixing data annotations and Fluent API across types; a migration to a new column type where unicode wasn't aligned.

Related errors


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