Detecting changed properties

I need to detect if a specific object property has changed in my OnXxxUpdated() partial method: what would be the best way to do this?

Hi @semafox,

The easiest way is probably to get the existing item from the DB and compare it to the updated one:

var existing = this.context.Categories.Where(o => o.CategoryID == item.CategoryID).FirstOrDefault();

if (existing.SomeProperty != item.SomeProperty)
{
   // SomeProperty has changed
}

This is how how I was trying to do it, but the values of the properties in existing and in item were the same…

Hi @semafox,

Indeed it seems that the EF context returns the updated entity without going to the DB in this case. If you use AsNoTracking() it should work though:

Step oldStep = this.context.Steps.AsNoTracking().Where(x => x.StepID == item.StepID).FirstOrDefault();
1 Like