Entity Framework Core Interview Question #105

How can you determine which properties of an entity are modified?

Change Tracking, Updates, Transactions & Concurrency Mid-Level Intermediate

Quick Interview Answer

Each tracked property entry exposes an IsModified flag. EF Core sets it when change detection identifies a modification, and application code can also set it explicitly.

Detailed Explanation

Each tracked property entry exposes an IsModified flag. EF Core sets it when change detection identifies a modification, and application code can also set it explicitly.

This is useful for auditing or targeted updates in disconnected scenarios. Explicitly marking only intended properties can help prevent overposting and unnecessary updates.

Care is required because incorrectly marking a property can overwrite database data.

Code Example

var entry = db.Entry(product);

foreach (var property in entry.Properties)
{
    if (property.IsModified)
        Console.WriteLine(property.Metadata.Name);
}