Optimistic locking breaking Auditing?

May 12, 2009 at 8:40 PM


I've had great luck using this fine tool in my MVC project, when following the Nerddinner recipe for insert/edits.



  public ActionResult Edit(int id, FormCollection fc)


         var foo = context.MyTypes.Single(d => d.id = id)

         UpdateModel( foo );




This makes the Audit system work perfectly, and if I change just one value on the web page, i get just one audit entry.


I then proceeded to include concurrency checks, using optimistic locking on a timestamp column.

This column (ROWVERSION), may not be altered, so I have to store it in the web page, and read it back during the save part of the Edit function  like so:


public ActionResult Edit(int id, MyType foo)


     MyType proxy = new MyType() {

                 Id = id,

                 Timestamp = ts


     context.Attach(foo, proxy);




This works, and gives me ChangeConflictExceptions if two people are editing the same MyType at once. The Auditing system, however gives me a changeset with all fields changed from "" to their actual value.

I'm guessing its because it is comparing proxy and foo, and only foo has been filled out (the new values).


I then tried

      context.Attach(foo, true)

To indicate that foo was indeed a changed object that was not currently in the system, but then the audit system returned all fields with the same value in both before and after.


Has anyone tried this and gotten it to work? What am I doing wrong?