Search code examples
c#entity-frameworkentity-framework-4change-trackingobjectstatemanager

Why does Entity Framework detect changes on properties which were modified but reset?


If I modify a property of a POCO Entity, but reset it the EntityFramework still says that there are changes.

Property "Name": Value "Test" (original value) 
              -> Value "Test123" (value changed by UI) 
              -> Value "Test" (value changed by UI to original value)

Entries that have been modified:

var objectStateEntries = 
    _db.ObjectStateManager.GetObjectStateEntries(
        EntityState.Added | 
        EntityState.Deleted | 
        EntityState.Modified);

How do you handle this case?


Solution

  • If all your properties are virtual Entity Framework will automatically create a dynamic proxy of your POCO by default. If I am not wrong change tracking is based in this case on property setters of this dynamic object, roughly like so:

    private string _name;
    public string Name
    {
        // ...
        set
        {
            // if (_name != value) such a check probably does not happen
            {
                _name = value;
                MarkPropertyAsModified(...);
            }
        }
    }
    

    So, there is no comparison with the original value, but only with the current value of the property. If this value gets changed the property is marked as modified, no matter if you reset it back to the original value.

    (Edit and correction of the previous paragraph: The property is marked as Modified if the setter is called, no matter if the same or a changed value is assigned. Thanks to Brad Thomas and his comment below!)

    You can avoid creating dynamic proxies by disabling this in the context options:

    objectContext.ContextOptions.ProxyCreationEnabled = false;
    

    Change detection will now rely on snapshot creation, which means that EF compares the original value (stored in a snapshot in the object context) with the current value when change detection is called. This does not happen anymore in property setters but inside of certain functions of Entity Framework, for example in SaveChanges. In your situation it would mean that when you call SaveChanges original value (snapshot) and current value will be the same because you did reset the change. Basically EF didn't notice that you changed the property twice and considers the property as unchanged.

    Be aware that disabling proxy creation - if you do it globally, for example in the context constructor - is a potentially deep change for your application. You might have code which depends on dynamic proxies in order to work correctly and it also can affect performance heavily in various situations. Dynamic proxies exist to make change tracking fast. Snapshot based change tracking is much slower.