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 = 
        EntityState.Added | 
        EntityState.Deleted | 

How do you handle this case?


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
    // ...
        // if (_name != value) such a check probably does not happen
            _name = value;

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.

Need Your Help

Call function in an array in a property of a class

php arrays class properties

Can someone please explain me why the following doesn't work, or give me a link which explains why? I can't find it here or on google.

Proprietary plug-ins for GPL programs: what about interpreted languages?

python open-source plugins licensing interpreted

I am developing a GPL-licensed application in Python and need to know if the GPL allows my program to use proprietary plug-ins. This is what the FSF has to say on the issue: