简体   繁体   English

ConflictResolver无法正常工作

[英]ConflictResolver is not working right

The ConflictResolver are : ConflictResolver是:

csp.ConflictResolver.ClientUpdateServerUpdateAction = ResolveAction.ClientWins;
csp.ConflictResolver.ClientUpdateServerDeleteAction = ResolveAction.ClientWins;

If the Client update and the Server update OR if the Client update and the Server delete. 如果客户端更新和服务器更新,或者如果客户端更新和服务器删除。 Everytime i press btnSyn button the server wins, but i want the client to win. 每次我按btnSyn按钮,服务器都会赢,但是我希望客户端赢。

Please, advise me. 请建议我。

private void LoadData()
{
  //dataGridView1.DataSource = ctx.Employees;
  ctx.Refresh(System.Data.Objects.RefreshMode.ClientWins, ctx.Employees);
  dataGridView1.DataSource = null;
  dataGridView1.DataSource = ctx.Employees;
}
private void btnSave_Click(object sender, EventArgs e)
{
  ctx.SaveChanges();
}

private void btnRefresh_Click(object sender, EventArgs e)
{
  LoadData();
}

private void btnSync_Click(object sender, EventArgs e)
{
  TaskTrackerDataEntityCacheSyncAgent syncAgent = new TaskTrackerDataEntityCacheSyncAgent();
  syncAgent.Employees.SyncDirection = SyncDirection.Bidirectional;
  var syncStats = syncAgent.Synchronize();

  TaskTrackerDataEntityCacheClientSyncProvider csp = new TaskTrackerDataEntityCacheClientSyncProvider();
  csp.ConflictResolver.ClientDeleteServerUpdateAction = ResolveAction.ServerWins;
  csp.ConflictResolver.ClientUpdateServerUpdateAction = ResolveAction.ClientWins;
  csp.ConflictResolver.ClientUpdateServerDeleteAction = ResolveAction.ClientWins;



  MessageBox.Show(String.Format("Uploaded/Downloaded: {0}/{1}{4}Uploads/Downloads Failed: {2}/{3}{4}", syncStats.TotalChangesUploaded, syncStats.TotalChangesDownloaded, syncStats.UploadChangesFailed, syncStats.DownloadChangesFailed, Environment.NewLine));
  LoadData();
}

It's really unclear what you're asking, what this code is supposed to do and what you have tried to resolve this, but you seem to create a SyncProvider and don't do anything with it. 确实不清楚您要问什么,该代码应该做什么以及您试图解决该问题的方法,但是您似乎创建了SyncProvider而不对其进行任何操作。

As I gather from a few web search hits for "SyncProvider apply ConflictResolver", you need to do something like this: 当我从“ SyncProvider apply ConflictResolver”的一些网络搜索命中收集信息时,您需要执行以下操作:

syncAgent.LocalProvider.ConflictResolver.ClientDeleteServerUpdateAction = ...
private void btnSync_Click(object sender, EventArgs e)
{
  //Create SyncAgent to have access to everything locally
  TaskTrackerDataEntityCacheSyncAgent syncAgent = new TaskTrackerDataEntityCacheSyncAgent();
  //=========================================================================================================
  //Determine the table and the direction for sync
  syncAgent.Employees.SyncDirection = SyncDirection.Bidirectional;
  //[Bidirectional( upload To the server first, then download from ther server )]
  //[DownloadOnly (Download from the server after full Sync )]
  //[Snapshot     ( Complete Refresh everything )]
  //[UploadOnly   ( upload to the server after full sync)]
  //=========================================================================================================
  //Create the ClientSyncProvider and ServerSyncProvider to use them in monitoring the failer in both sides
  TaskTrackerDataEntityCacheServerSyncProvider ssp = new TaskTrackerDataEntityCacheServerSyncProvider();
  TaskTrackerDataEntityCacheClientSyncProvider csp = new TaskTrackerDataEntityCacheClientSyncProvider();
  //=========================================================================================================
  //The SqlCeClientSyncProvider also includes a ConflictResolver property that you can use to resolve conflicts on the client. For each type of conflict, you can set a value from the ResolveAction enumeration:
  //ClientWins, ServerWins, FireEvent

  //There is no requirement to set the ConflictResolver for each type of conflict. 
  //You can resolve conflicts as you do on the server, by handling the ApplyChangeFailed event
  csp.ConflictResolver.ClientDeleteServerUpdateAction = ResolveAction.ClientWins;
  csp.ConflictResolver.ClientUpdateServerUpdateAction = ResolveAction.ClientWins;
  csp.ConflictResolver.ClientUpdateServerDeleteAction = ResolveAction.ClientWins;
  csp.ConflictResolver.ClientDeleteServerUpdateAction = ResolveAction.ClientWins;
  csp.ConflictResolver.ClientInsertServerInsertAction = ResolveAction.ClientWins;
  csp.ConflictResolver.StoreErrorAction = ResolveAction.ClientWins;
  csp.ApplyChangeFailed += SampleClientSyncProvider_ApplyChangeFailed;
  ssp.ApplyChangeFailed += SampleClientSyncProvider_ApplyChangeFailed;
  //=========================================================================================================
  //Add the  ClientSyncProvider and ServerSyncProvider to the syncAgnet
  syncAgent.LocalProvider = csp;
  syncAgent.RemoteProvider = ssp;   //it will help to generate exception when the client update refused 
  //=========================================================================================================
  //Call Sync method and recieve the a report about it
  SyncStatistics synStats = syncAgent.Synchronize();
  //=========================================================================================================
  //Display the sync stats
  MessageBox.Show(String.Format("Uploaded/Downloaded:{0}/{1}{4}", synStats.TotalChangesUploaded, synStats.TotalChangesDownloaded, synStats.UploadChangesFailed, synStats.DownloadChangesFailed, Environment.NewLine));
  //==============================================================
  //LoadData(); //It is function to reload the data 
}
//================================================================
//This function will run the client update over server update
private void SampleClientSyncProvider_ApplyChangeFailed(object sender, ApplyChangeFailedEventArgs e)
{

  //Log event data from the client side.
  //EventLogger.LogEvents(sender, e);


  if (e.Conflict.ConflictType == ConflictType.ClientInsertServerInsert)
  {
    //e.Action = ApplyAction.Continue;          // ignore the conflict and continue synchronization.
    //e.Action = ApplyAction.RetryApplyingRow;    //retry applying the row. The retry will fail, and the event will be raised again if you do not address the cause of the conflict by changing one or both of the conflicting rows.
    e.Action = ApplyAction.RetryWithForceWrite; //retry with logic to force applying the change.
  }
  if (e.Conflict.ConflictType == ConflictType.ClientDeleteServerUpdate)
  {
    //e.Action = ApplyAction.Continue;          // ignore the conflict and continue synchronization.
    //e.Action = ApplyAction.RetryApplyingRow;    //retry applying the row. The retry will fail, and the event will be raised again if you do not address the cause of the conflict by changing one or both of the conflicting rows.
    e.Action = ApplyAction.RetryWithForceWrite; //retry with logic to force applying the change.
  } if (e.Conflict.ConflictType == ConflictType.ClientUpdateServerDelete)
  {
    //e.Action = ApplyAction.Continue;          // ignore the conflict and continue synchronization.
    //e.Action = ApplyAction.RetryApplyingRow;    //retry applying the row. The retry will fail, and the event will be raised again if you do not address the cause of the conflict by changing one or both of the conflicting rows.
    e.Action = ApplyAction.RetryWithForceWrite;//retry with logic to force applying the change.
  }
  if (e.Conflict.ConflictType == ConflictType.ClientUpdateServerUpdate)
  {
    //e.Action = ApplyAction.Continue;          // ignore the conflict and continue synchronization.
    //e.Action = ApplyAction.RetryApplyingRow;    //retry applying the row. The retry will fail, and the event will be raised again if you do not address the cause of the conflict by changing one or both of the conflicting rows.
    e.Action = ApplyAction.RetryWithForceWrite; //retry with logic to force applying the change.
    //Logic goes here.
  }

  if (e.Conflict.ConflictType == ConflictType.ErrorsOccurred)
  {
    //Logic goes here.

  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM