EF Core detects changes by comparing each property's current value with the original value snapshot taken at load time. For immutable types (string, int, records you never mutate) copying the reference is a perfect snapshot. For a mutable type mapped through a value converter, such as a List<string> stored as JSON or a mutable class, the snapshot is the same object: change it in place (tags.Add("x")) and current and original are still equal, so the update is silently lost. A ValueComparer<T> tells EF Core three things: how to compare two values for equality, how to compute a hash code, and how to take a snapshot that is an independent copy (for a list, v => v.ToList()). EF Core 8+ supplies comparers for primitive collections automatically, but your own converters need one. A good comparer also avoids pointless UPDATEs, because equal values are no longer seen as modified.
"EF Core's default change tracking is snapshot-based. When it loads an entity it stores the original value of every property, and at
DetectChangesit compares current against original. For value types and immutable references likestring, storing the reference is enough, because the only way to change the value is to assign a new instance.That breaks for mutable types behind a value converter. Say I map
List<string> Tagsto a JSON string withHasConversion. The snapshot is the sameListobject, so when code callsTags.Add(\"urgent\"), original and current are both the modified list, EF Core sees no change andSaveChangeswrites nothing. No error, just lost data.A
ValueComparer<T>fixes that with three expressions: equality (for a list,SequenceEqual), hash code, and a snapshot function that makes a real copy,v => v.ToList(). Now the snapshot is independent and in-place edits are detected. The same equality expression also stops EF Core issuing updates when a value was reassigned but is logically equal. EF Core 8 and later handle primitive collections likeList<string>natively with their own comparer; I need to write one for my own converters and mutable value objects, or better, make the value object immutable."
var tagsComparer = new ValueComparer<List<string>>(
(a, b) => a!.SequenceEqual(b!),
v => v.Aggregate(0, (h, s) => HashCode.Combine(h, s.GetHashCode())),
v => v.ToList()); // independent copy as the snapshot
modelBuilder.Entity<Ticket>()
.Property(t => t.Tags)
.HasConversion(
v => JsonSerializer.Serialize(v, (JsonSerializerOptions?)null),
v => JsonSerializer.Deserialize<List<string>>(v, (JsonSerializerOptions?)null) ?? new())
.Metadata.SetValueComparer(tagsComparer);
| Property type | Comparer needed? |
|---|---|
int, string, DateTimeOffset |
No |
| Immutable record via converter | No (default equality works) |
| Primitive collection mapped natively (EF Core 8+) | No, built in |
List<T>/mutable class via your own converter |
Yes |
byte[] |
Built-in comparer already compares contents |
A ticket system lost [~3%] of tag edits for weeks because tags were added in place to a converted list; adding a comparer fixed it, and a test that adds a tag, saves, and reloads now guards it.
Checks understanding of snapshot change tracking.
Last updated 23 Nov 2026.