Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework: Cancel a property change if no change in value

When setting a property on an entity object, it is saving the value to the database even if the value is exactly the same as it was before. Is there anyway to prevent this?

Example:

If I load a Movie object and the Title is "A", if I set the Title to "A" again and SaveChanges() I was hoping that I wouldn't see the UPDATE statement in SqlProfiler but I am. Is there anyway to stop this?

like image 577
Rick Glos Avatar asked Jan 21 '09 01:01

Rick Glos


2 Answers

Yes, you can change this. Doing so isn't trivial, however, in the current version of the Entity Framework. It will become easier in the future.

The reason you're seeing this behavior is because of the default code generation for the entity model. Here is a representative example:

public global::System.Guid Id
{
    get
    {
        return this._Id;
    }
    set
    {
        // always!
        this.OnIdChanging(value);
        this.ReportPropertyChanging("Id");
        this._Id = global::System.Data.Objects.DataClasses
                               .StructuralObject.SetValidValue(value);
        this.ReportPropertyChanged("Id");
        this.OnIdChanged();
    }
}
private global::System.Guid _Id;
partial void OnIdChanging(global::System.Guid value);
partial void OnIdChanged();

This default code generation is reasonable, because the Entity Framework doesn't know the semantics of how you intend to use the values. The types in the property may or may not be comparable, and even if they are, the framework can't know how you intend to use reference equality versus value equality in all cases. For certain value types like decimal, it's pretty clear, but in a general sense it's not obvious.

You, on the other hand, know your code, and can customize this some. The trouble is that this is generated code, so you can't just go in and edit it. You need to either take over the code generation, or make it unnecessary. So let's look at the three options.

Take over the code generation

The essential approach here is to create a T4 template which does the code behind, and that the default code generation from the Entity Framework. Here is one example. One advantage of this approach is that the Entity Framework will be moving to T4 generation in the next version, so your template will probably work well in future versions.

Eliminate code generation

The second approach would be to eliminate cogeneration altogether, and do your change tracking support manually, via IPOCO. Instead of changing how the code is generated, with this approach you don't do any code generation at all, and instead provide change tracking support to the Entity Framework by implementing several interfaces. See the linked post for more detail.

Wait

Another option is to live with the Entity Framework the way it is for the time being, and wait until the next release to get the behavior you desire. The next version of the Entity Framework will use T4 by default, so customizing the code generation will be very easy.

like image 91
Craig Stuntz Avatar answered Nov 03 '22 00:11

Craig Stuntz


According to MSDN:

The state of an object is changed from Unchanged to Modified whenever a property setter is called. This occurs even when the value being set is the same as the current value. After the AcceptAllChanges method is called, the state is returned to Unchanged. By default, AcceptAllChanges is called during the SaveChanges operation.

Looks like you'll want to check the value of properties on your Entity objects before you update to prevent the UPDATE statement.

like image 20
Dave Swersky Avatar answered Nov 03 '22 01:11

Dave Swersky