我有一个使用WCF RIA Services RTM的Silverlight 4项目。大多数RIA功能都在工作,但我遇到了并发检查的问题。服务器正在检查并发性并将DomainOperationException传递给DomainDataSource.SubmittedChanges事件。我正在处理它甚至枚举EntitiesInError。然后我在EntityConflict上调用Resolve。这似乎是更新实体的“旧值”,因此我可以重新提交它们,但客户端的更改将保留在实体中。我宁愿消除客户的变化,让他们重新做,或者最终告诉他们改变了什么,让他们选择保留。下面是我到目前为止的代码示例。我发现这篇文章:http://sklementiev.blogspot.com/2010/03/wcf-ria-and-concurrency.html但它似乎不适用于RIA Services RTM。感谢。
代码示例:
Private Sub dds_SubmittedChanges(ByVal sender As Object, ByVal e As System.Windows.Controls.SubmittedChangesEventArgs)
If e.HasError Then
If TypeOf e.Error Is DomainOperationException Then
handleDomainOperationException(sender, e, "myType")
End If
End If
End Sub
Private Sub handleDomainOperationException(ByVal sender As Object, ByVal e As SubmittedChangesEventArgs, ByVal entityType As String)
Dim dds As DomainDataSource = DirectCast(sender, DomainDataSource)
Select Case DirectCast(e.Error, DomainOperationException).Status
Case OperationErrorStatus.Conflicts
ErrorWindow.CreateNew(String.Format("Another user updated this {0} between the time that you viewed it and when you submitted your changes. Your changes have been reverted. Please make your changes again and re-submit.", entityType))
For Each ent In e.EntitiesInError
If Not ent.EntityConflict.IsDeleted Then
'tried this, doesn't overwrite changes, just updates old fields
ent.EntityConflict.Resolve()
Else
Throw New Exception("This entity has already been deleted.")
End If
Next
e.MarkErrorAsHandled()
Case OperationErrorStatus.ValidationFailed
ErrorWindow.CreateNew("Data validation failed")
End Select
End Sub
答案 0 :(得分:1)
我强烈建议你看一下Yacine Khammal关于处理并发冲突的Pluralsight培训,它有一个很好的解决方案here(付费墙,但每月29美元,价值很多倍)。请参阅名为“演示:处理验证和并发错误”的视频。我找不到任何关于此类文件的文档。这个解决方案的理想之处在于它通过变更集在EF层之上运行,并允许您通过粒度识别已更改的内容。您可以提示用户通知他们加载和编辑的内容与服务器上的内容不同,让他们决定是否要覆盖或恢复到最新版本。