开发者

Entity framework entity is in modified state (and push an update) although affecting same value?

I will explain with an example:

//get an entity
var myEntity context.GetClient(543622);

// imagine this client has already a property myValue=10 
//    (retrieved from the database)
//set again this value
myEntity.myValue = 10;

// then we get the modif开发者_JAVA技巧ied for this entity
ObjectStateEntry ose = null;
_context.ObjectStateManager.TryGetObjectStateEntry(myEntity.EntityKey, out ose);

// the ose.EntityState is modified
// and here count = 1 ??!!...
var count = ose.GetModifiedProperties().Count();


//at last a SaveChanges push a commit to the bdd 
//    (tracked with SQL Server Profiler).
_context.SaveChanges();

It seems that whatever you set a same value to an entity preperty, the state change to modified and an update is pushed to the DB. I am surprised of a such behaviour...

Sinn'


It seems a normal behavior: http://msdn.microsoft.com/en-us/library/bb738695%28v=vs.90%29.aspx

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.

If you look at the behind code of an entity property generated by EF:

set
  {
    this.OnNomChanging(value);
    this.ReportPropertyChanging("Nom");
    this._Nom = global::System.Data.Objects.DataClasses.StructuralObject.SetValidValue(value, false);
    this.ReportPropertyChanged("Nom");
    this.OnNomChanged();
  }

The ReportPropertyChanged method change the state of the entity whatever the value is/was.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜