Entity Framework: discard property change if value change

When setting a property on an entity object, it stores the value in the database, even if the value is exactly the same as before. Is there any way to prevent this?

Example:

If I load a Movie object and Title is "A", if I set Title to "A" again and SaveChanges (), I hoped that I would not see the UPDATE statement in SqlProfiler, but I am. Anyway, to stop this?

+5
source share
4 answers

Yes, you can change that. However, this is not trivial in the current version of the Entity Framework. It will become easier in the future.

The reason you see this behavior is due to the default code generation for the entity model. Here is an 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();

, Entity Framework , . , , , . , , , .

, . , , . , . .

T4, , Entity Framework. . , Entity Framework T4 , , , .

- , IPOCO. , , Entity Framework . . .

- Entity Framework , , , . Entity Framework T4 , .

+5

MSDN:

, setter. . AcceptAllChanges, . default, AcceptAllChanges SaveChanges.

, Entity , UPDATE.

+4

, INotifyPropertyChanged, , PropertyChanged , . : -

    public decimal Value
    {
        get
        {
            return _value;
        }
        set
        {
            if (_value != value)
            {
                _value = value;
                if (_propertyChanged != null) _propertyChanged(this, new PropertyChangedEventArgs("Value"));
            }
        }

    }

, Entity Framework.

0

, , - , , :

public sealed partial class MyEFType {
   public string MyWrappedProperty {
      get {
         return MyProperty;
      }
      set {
         if (value == MyProperty)
            return;
         MyProperty = value;
      }
   }
}

, , , , - .

0

All Articles