Conflict handling in DomainContext
I have a Linq-to-SQL RIA service and a silverlight client. When 2 people edit the same entity I receive an EntityConflict in the OnSubmitCompleted when calling SubmitChanges. Now I want to react to it without annoying the user too much, I only want to show him a message box and let him know that the data has been modified and that his changes will be overridden with the server values.
MyDomainContext.SubmitChanges(this.OnSubmitCompleted, invokeAfterSubmit);
private void OnSubmitCompleted(SubmitOperation so)
{
if (so.HasError)
{
if (so.EntitiesInError.Any(entity => entity.EntityConflict != null))
{
MessageBox.Show("Data has changed, entities will be reloaded....","Error", MessageBoxButton.OK);
// cache the entities, because the AcceptChanges() will clear them
var erro开发者_StackOverflowrEntities = so.EntitiesInError.ToList();
// overwrite the local changes with the ones from the stoe
((IChangeTracking)Context.EntityContainer).AcceptChanges();
// reload the data
-> here comes the dirty code from below
// mark as handled as in "The User has been informed that all his stuff has been reverted"
so.MarkErrorAsHandled();
}
}
I have no idea how to reload the data for this specific entity. I would not even mind reloading that whole data again.
At the moment I am doing this, and I feel very dirty :)
foreach (Entity entity in errorEntities)
{
var methodInfo = (from method in Context.GetType().GetMethods()
where method.Name.Contains("Query")
where method.Name.Contains(entity.GetType().Name)
select method);
foreach (var info in methodInfo)
{
// get the query from the Context to reload the entity
var result = (EntityQuery) info.Invoke(Context, null);
// load the entities
Context.Load(result, LoadBehavior.RefreshCurrent, null, null);
}
}
This works, but I am sure there are better ways to resolve conflicts. (Merging would of course be great!)
Please let me know of better ways to handle this scenario :)
This problem with the conflicts can be solved on the server. The domain service is a partial class where you can override the method ResolveConflicts
.
Example:
protected override bool ResolveConflicts(ChangeConflictCollection conflicts)
{
bool resolveChangeSetSuccess = true;
foreach (ObjectChangeConflict objectChangeConflict in conflicts)
{
foreach (MemberChangeConflict memberChangeConflict in objectChangeConflict.MemberConflicts)
{
if (memberChangeConflict.Member.Name == "Name of DB field")
{
memberChangeConflict.Resolve(RefreshMode.KeepCurrentValues);
}
else
{
memberChangeConflict.Resolve(RefreshMode.KeepChanges);
}
}
resolveChangeSetSuccess = resolveChangeSetSuccess && objectChangeConflict.IsResolved;
}
if (resolveChangeSetSuccess)
{
this.DataContext.SubmitChanges();
}
return resolveChangeSetSuccess;
}
精彩评论