📁 Sangam Dhakal - Portfolio v2.0 - Netscape Navigator 4.0
_
□
×

📚 Interview question

When does EF Core need a ValueComparer, and what goes wrong without one?

Mid-Level Coding Intermediate

🎯 Key points

  • Change detection compares current values with an original-value snapshot
  • Mutable converted types share the snapshot object → in-place edits not detected
  • ValueComparer<T>(equals, hashCode, snapshot)
  • Snapshot must be a deep copy, e.g. v => v.ToList()
  • Immutable types need none: a new value means a new instance
  • Primitive collections (EF Core 8+) get comparers automatically; custom converters do not
  • Correct equality also prevents unnecessary UPDATE statements

⏱ 30-second answer

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.

🗣 Full answer (senior)

How I would answer this in an interview

"EF Core's default change tracking is snapshot-based. When it loads an entity it stores the original value of every property, and at DetectChanges it compares current against original. For value types and immutable references like string, 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> Tags to a JSON string with HasConversion. The snapshot is the same List object, so when code calls Tags.Add(\"urgent\"), original and current are both the modified list, EF Core sees no change and SaveChanges writes 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 like List<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);

When you need one

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

Production example

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.

🔍 What the interviewer is checking

Checks understanding of snapshot change tracking.

↩ Usually asked after

More in EF Core Fundamentals

Last updated 23 Nov 2026.

🌐 Sangam's Webring

⏪ Previous 🏠 Main Next ⏩
💬 Chat with me