0

Note Solved this has been edited More information check the edits

When performing a post back with an entities model in MVC. The model binder neglects to bind the original values. So if anyone is using a derivative of save changes to audit log (whether that be Overriding or tying into the events) there is a high possibility for it to not log you changes correctly to fix this you can fix the problem by using this function which clones the current values, reloads the object, and then reset the current values.

    void SetCorrectOriginalValues(DbEntityEntry Modified)
    {
        var values = Modified.CurrentValues.Clone();
        Modified.Reload();
        Modified.CurrentValues.SetValues(values);
        Modified.State = EntityState.Modified;
    }

You can gain access to the DbEntityEntry though the change tracker, or the entry function from your context. If there are any improvement or things I'm missing out let me know so I can correct them.

johnny 5
  • 19,893
  • 50
  • 121
  • 195
  • Wow, that's a lot of text and code! It would help if you could try to minimize your issue: reproduce the case so that it still shows your problem, but with as little other code around it as possible. – Martijn Sep 23 '14 at 19:29
  • I added all of the code because when I don't I usually get down voted. I'm just trying to write an entities method that uses the object submitted to grab the original values in the database before updating the entity so I can Track the changes. – johnny 5 Sep 24 '14 at 13:35

0 Answers0