AspNetCoreLocalization icon indicating copy to clipboard operation
AspNetCoreLocalization copied to clipboard

Case sensitivity issue with .NET 8 onward

Open daniel-jann opened this issue 1 month ago • 0 comments

.NET 8 DbContext uses case insensitive fields with SQL Server, which can lead to failures when using keys that only differ in casing.

As described by ajcvickers, case sensitive comparers should be used:

// From .NET 8 onwards, string comparisons are case insensitive by default in EF Core.
// We need to explicitly set case sensitive comparers for Key and ResourceKey properties.
var caseSensitiveComparer = new ValueComparer<string>(
    (l, r) => string.Equals(l, r, StringComparison.Ordinal),
    v => v.GetHashCode(),
    v => v);

builder.Entity<LocalizationRecord>().Property(m => m.Key).Metadata.SetValueComparer(caseSensitiveComparer);
builder.Entity<LocalizationRecord>().Property(m => m.ResourceKey).Metadata.SetValueComparer(caseSensitiveComparer);

daniel-jann avatar Dec 11 '25 17:12 daniel-jann