I'm using this code to update an object (MySQL) that we have received via AJAX from the front-end:
foreach (ChequeDiaryPaymentDetail item in data.Updated) {
this.DBContext.ChequeDiaryPaymentDetails.Attach(item);
this.DBContext.ApplyOriginalValues("ChequeDiaryPaymentDetails", item);
}
Is this the correct method? If I do the same thing, except substitute the
ApplyOriginalValues
for
this.DBContext.ObjectStateManag开发者_StackOverflow社区er.ChangeObjectState(item, System.Data.EntityState.Modified);
.. the DB is updated correctly too. Is the latter method incorrectly forcing an update? I just want to make sure I'm not doing it incorrectly from the outset!
If your object is in the Unchanged state, then the ApplyOriginalValues method will change the object state to Modified (just as the ChangeObjectState method). I suppose this is the case.
So, if your logic needs to apply original values before performing an update (you are implementing an Optimistic Concurrency model, for example), the better solution will be to apply original values first. Otherwise it should be enough to change the object state only.
精彩评论