Linq datacontext GetModifiedMembers in Attach script
I am trying to implement optimistic locking in an asp.net MVC application and also provide trailing auditing.
The audit framework is based on the ability to call DataContext.GetModifiedMembers during SubmitChanges, which makes sense in my opinion.
Optimistic locking uses ROWVERSION timestamps serialized to base64 and placed in a hidden field in the view.
The My Edit action looks like this:
[AcceptVerb(HttpVerb.Post)]
public ActionResult Edit(MyType myType)
{
context.MyTypes.Attach(myType, true);
context.SubmitChanges(ConflictMode.FailOnFirstConflict);
}
With this, DataContext.GetModifiedMembers will always return ALL properties in MyType, not just those that have changed between the database and the provided values, which violates auditing. Specifically, it returns each property as it was changed from a new value to a new value, so it doesn't even like doing anything smart on the list.
I tried loading the object first before attaching it, but that gives a duplicate key exception.
Then I tried to use UpdateModel i.e.
[AcceptVerb(HttpVerb.Post)]
public ActionResult Edit(int id, FormCollection col)
{
var mt = context.MyTypes.Single( mt => mt.id = id);
UpdateModel(mt);
context.SubmitChanges(ConflictMode.FailOnFirstConflict);
}
This works with auditing, but optimistic locking does not. Instead of a ChangeConflict exception, I get an InvalidOperationException because the UpdateModel changes the concurrentTS field (which appears to be readonly).
What am I doing wrong?
a source to share