Skip to content

PendingModelChangesWarning Diacritic Café #38191

Closed

Description

@mike-jewell

Bug description

This only occurs when running a migration on Linux. Windows appears to be ok with this. I am currently testing this in WSL2 with the Ubuntu distro. When I run my test suite on WSL2 I get the PendingModelChangesWarning. I have added a migration and found that its a diacritic unicode character that isn't recognised as being a match. The output of the migration is shown in the code section.

I recognise that this isn't technically a dotnet ef bug, but I don't know how the diff occurs in order to fix it on my environment and would like to know a solution. Hopefully making this useful for someone else. I have gone through the other issues, but I cannot find a similar issue.

Your code

using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace XXXX.Migrations
{
    /// <inheritdoc />
    public partial class Test : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.UpdateData(
                table: "BusinessTypes",
                keyColumn: "BusinessTypeId",
                keyValue: 28,
                column: "WebDescription",
                value: "Caf�");

            migrationBuilder.UpdateData(
                table: "BusinessTypes",
                keyColumn: "BusinessTypeId",
                keyValue: 29,
                column: "WebDescription",
                value: "Caf� & bar");
        }

        /// <inheritdoc />
        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.UpdateData(
                table: "BusinessTypes",
                keyColumn: "BusinessTypeId",
                keyValue: 28,
                column: "WebDescription",
                value: "Café");

            migrationBuilder.UpdateData(
                table: "BusinessTypes",
                keyColumn: "BusinessTypeId",
                keyValue: 29,
                column: "WebDescription",
                value: "Café & bar");
        }
    }
}

Stack traces


Verbose output


EF Core version

10.0.0

Database provider

No response

Target framework

No response

Operating system

Windows 11 & WSL2.0 Ubuntu

IDE

Visual Studio 2026 18.5.2

Activity

  1. mike-jewell commented on Apr 29, 2026

    @mike-jewell
    Author

    I'm not so sure it is the OS that is the issue....

    I am able to create a file with a diacritic in it no problem:

    Image
  2. TomasMoralesBarr commented on Apr 29, 2026

    @TomasMoralesBarr
    Contributor

    Root Cause Analysis

    This bug is caused by a Unicode normalization mismatch between Windows and Linux.

    The character é in "Café" can be represented in two valid Unicode forms:

    Form Code Description
    NFC (composed) \u00E9 A single é character — default on Windows
    NFD (decomposed) e + \u0301 The letter e plus a combining accent — common on Linux/macOS

    When the migration snapshot is generated on Windows (NFC) and HasPendingModelChanges() is later executed on Linux (NFD), EF Core detects a difference that does not semantically exist, triggering the PendingModelChangesWarning and scaffolding a corrupt/empty migration.


    Source of the Problem

    The issue lives in MigrationsModelDiffer.cs, in the seed data comparison logic:

    if (!Equals(existingColumnModification.Value, value))
    {
        // Detects a "change" and scaffolds a false migration
    }

    Equals() on strings performs a byte-by-byte (ordinal) comparison, so "Café" in NFC ≠ "Café" in NFD, even though they are visually and semantically identical.


    Proposed Fix

    The fix is small and self-contained (~5–10 lines in a single file). String values should be normalized to NFC before being compared, using .NET's built-in API:

    // Before
    if (!Equals(existingColumnModification.Value, value))
    
    // After
    if (!NormalizedEquals(existingColumnModification.Value, value))
    
    // Suggested helper
    private static bool NormalizedEquals(object? a, object? b)
    {
        if (a is string sa && b is string sb)
        {
            return string.Equals(
                sa.Normalize(NormalizationForm.FormC),
                sb.Normalize(NormalizationForm.FormC),
                StringComparison.Ordinal);
        }
        return Equals(a, b);
    }

    This ensures that seed data comparison is OS-independent, regardless of the default Unicode normalization used by the underlying platform.


    Immediate Workaround

    While the official fix is not yet available, you can explicitly force NFC normalization in your OnModelCreating. This guarantees that the value stored in both the model and the snapshot is always byte-for-byte identical across platforms:

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<BusinessType>().HasData(
            new BusinessType
            {
                BusinessTypeId = 28,
                WebDescription = "Café".Normalize(NormalizationForm.FormC)
            },
            new BusinessType
            {
                BusinessTypeId = 29,
                WebDescription = "Café & bar".Normalize(NormalizationForm.FormC)
            }
        );
    }

    Why does this work? By explicitly calling .Normalize(NormalizationForm.FormC) in your source code, both Windows and Linux will produce the exact same byte sequence at compile and runtime. EF Core will no longer see any difference between the model and the snapshot.

    Optionally, you can define a small extension method to keep things clean if you have many strings:

    public static string NFC(this string value)
        => value.Normalize(NormalizationForm.FormC);
    
    // Usage
    WebDescription = "Café".NFC()

  3. TomasMoralesBarr commented on Apr 29, 2026

    @TomasMoralesBarr
    Contributor

    I would like to work on a fix.

  4. added theissue type on Apr 29, 2026
  5. added this to the 11.0.0 milestone on Apr 29, 2026
  6. self-assigned this
    on Apr 29, 2026
  7. roji commented on Apr 30, 2026

    @roji
    Member

    AI Triage

    The below is an AI-generated analysis and may contain inaccuracies.

    Summary

    This is a confirmed bug in EF Core's seed data comparison logic. When the same Unicode character (e.g. é) is represented in NFC (precomposed: U+00E9) vs NFD (decomposed: U+0065 + U+0301) between the model snapshot and the current model, EF Core falsely detects a pending model change.

    Type: Bug
    Area: area-migrations-seeding (already labeled correctly)

    Root Cause

    The seed data comparison in MigrationsModelDiffer.cs at line 2217 uses the ProviderValueComparer, which for strings defaults to ordinal equality (==). Ordinal comparison is byte-sensitive and does not account for Unicode canonical equivalence — NFC "Café" (5 UTF-8 bytes: 43-61-66-C3-A9) ≠ NFD "Café" (6 UTF-8 bytes: 43-61-66-65-CC-81), even though they represent the identical logical string.

    Windows vs. Linux Behavior

    The claim that "Windows is ok but Linux isn't" is likely a coincidence of how the source files were authored, not a runtime behavioral difference. Key findings:

    1. object.Equals / string.== is ordinal and platform-independent — it would fail on both platforms given different normalization forms.
    2. The C# compiler (Roslyn) does not normalize Unicode in string literals — the bytes in the source file are exactly what appears in IL.
    3. Git does not normalize Unicode in file content (unlike filenames on macOS HFS+/APFS).

    The most likely scenario is:

    • The model code (HasData("Café")) was authored with one normalization form (likely depends on the editor/input method/OS used when typing).
    • The migration snapshot was generated on a different platform or after an edit that changed the normalization form.
    • On whichever platform the user last regenerated the snapshot, the values happen to match. On the other platform where they only run (without regenerating), the mismatch manifests.

    Regarding StringComparison as a Fix

    I tested whether any StringComparison enum value handles NFC/NFD equivalence:

    Comparison NFC "é" == NFD "é"? Notes
    Ordinal ❌ Byte-by-byte, normalization-blind
    InvariantCulture ✅ ICU/NLS handles canonical equivalence
    CurrentCulture ✅ Same as above, culture-dependent

    StringComparison.InvariantCulture does correctly treat NFC and NFD as equal, and does NOT introduce unwanted collapsing (e.g. ß ≠ ss, case is preserved). This is because the Unicode Collation Algorithm used by ICU/NLS inherently handles canonical equivalence.

    However, for a fix in EF Core I would still recommend explicit normalization to NFC before ordinal comparison rather than switching to culture-aware comparison, because:

    1. Ordinal comparison semantics are clearer and more predictable for database values.
    2. Culture-aware behavior can subtly differ across .NET versions and ICU/NLS implementations.
    3. There is already precedent in this file: MultilineEquals() normalizes line endings before ordinal comparison — the same pattern (normalize form, then compare ordinally) should be applied for Unicode normalization.

    A possible fix would normalize string values to NFC before comparison in the seed data diff path (around line 2217 and/or wherever ProviderValueComparer.Equals is called for seed data values).

    No Duplicates Found

    No existing open or closed issues in the EF Core repo match this specific scenario.

  8. roji commented on Apr 30, 2026

    @roji
    Member

    I'm personally leaning in the direction of using InvariantCulture rather than calling Normalize() - that's exactly what it's there for, and I'm not sure why it would "subtly differ across .NET versions and ICU/NLS implementations" any more than Normailze(). But no strong feelings.

    @AndriySvyryd assigning this and #38197 to you as it's in your area.

  9. roji commented on Apr 30, 2026

    @roji
    Member

    Additional AI note: why NFC normalization over StringComparison.InvariantCulture

    InvariantCulture comparison does more than handle canonical equivalence (NFC↔NFD) — it also ignores "default ignorable" characters like soft hyphen (U+00AD), zero-width space (U+200B), etc.:

    string.Equals("Caf\u00ADé", "Café", StringComparison.InvariantCulture) // TRUE
    // But these are different byte sequences that produce different stored values in the database!

    With explicit NFC normalization + ordinal comparison, only canonically equivalent representations are collapsed — nothing else. Characters that are distinct bytes in the database remain distinct in the comparison.

    That said, the ignorable-character scenario is somewhat edge-case for typical seed data, so InvariantCulture would work for the common case and is simpler code. It's a judgment call on how defensively to code the fix.

  10. roji commented on Apr 30, 2026

    @roji
    Member

    @AndriySvyryd just noting that we should keep change tracking in mind here too. In other words, if non-normalized string comparison affects string comparisons in HasData(), does it also affect change tracking in an adverse way? Should the two be aligned?

  11. added a commit that references this issue on May 4, 2026
    2b20b9b
  12. added a commit that references this issue on Jul 13, 2026
    a56eb22
  13. added a commit that references this issue on Jul 16, 2026
    40ea93d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions