附加方案中的linq datacontext GetModifiedMembers

时间:2021-05-25 06:51:41

I am trying to implement Optimistic Locking in an asp.net MVC application, as well as provide audit trailing.

我试图在asp.net MVC应用程序中实现Optimistic Locking,并提供审计跟踪。

The audit framework relies on being able to call DataContext.GetModifiedMembers during SubmitChanges, which makes good sense, i guess.

审计框架依赖于能够在SubmitChanges期间调用DataContext.GetModifiedMembers,这是有道理的,我猜。

The optimistic locking uses ROWVERSION timestamps, serialized to base64 and put in a hidden field in the view.

乐观锁定使用ROWVERSION时间戳,序列化为base64并放入视图中的隐藏字段。

My Edit action looks like this:

我的编辑操作如下所示:

[AcceptVerb(HttpVerb.Post)] 
public ActionResult Edit(MyType myType)
{ 
   context.MyTypes.Attach(myType, true);
   context.SubmitChanges(ConflictMode.FailOnFirstConflict);
}

When doing this, the DataContext.GetModifiedMembers will always return ALL the properties on MyType, rather than just the ones that are changed between the database and the values provided, which breaks the auditing. Specifically it returns every property as having been changed from their new value to their new value, so its not even like I can do anything clever to the list.

执行此操作时,DataContext.GetModifiedMembers将始终返回MyType上的所有属性,而不仅仅是在数据库和提供的值之间更改的属性,这会破坏审计。具体来说,它将每个属性从其新值更改为新值,因此它甚至不能像我可以对列表做任何聪明的事情。

I tried loading the object first, before attaching it, but this gives a duplicate key exception.

我尝试先加载对象,然后再附加它,但这会产生一个重复的键异常。

I then tried using UpdateModel, i.e.

然后我尝试使用UpdateModel,即

[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 the auditing, but fails the optimistic locking. Rather than a ChangeConflictException i get an InvalidOperationException because the UpdateModel is changing the concurrentTS field (which apparently is readonly).

这适用于审计,但未通过乐观锁定。而不是ChangeConflictException我得到一个InvalidOperationException,因为UpdateModel正在更改concurrentTS字段(显然是readonly)。

What am I doing wrong?

我究竟做错了什么?

1 个解决方案

#1


Progress so far consists of doing the last part, and catching InvalidOperationException and looking for the text "Value of member 'ConcurrencyTimestamp'", and rethrowing that as a ChangeConflictException.

到目前为止的进展包括完成最后一部分,捕获InvalidOperationException并查找文本“成员的值'ConcurrencyTimestamp'”,并将其重新抛出为ChangeConflictException。

That seems to do the trick, but it is not pretty.

这似乎可以解决问题,但它并不漂亮。

#1


Progress so far consists of doing the last part, and catching InvalidOperationException and looking for the text "Value of member 'ConcurrencyTimestamp'", and rethrowing that as a ChangeConflictException.

到目前为止的进展包括完成最后一部分,捕获InvalidOperationException并查找文本“成员的值'ConcurrencyTimestamp'”,并将其重新抛出为ChangeConflictException。

That seems to do the trick, but it is not pretty.

这似乎可以解决问题,但它并不漂亮。