Microsoft Sync Framework - 范围和跟踪列值

时间:2013-10-09 15:20:49

标签: sql sql-server sync microsoft-sync-framework

我的任务是弄清楚这个系统是如何工作的,而且在谈到这些问题时我几乎不是新手,所以要善待。

我已经同步并运行了一个示例数据库进行测试。我正在使用Visual Studio,我在C#中。以下是我当前提供同步的代码。

        // define a new scope named ProductsScope
        DbSyncScopeDescription scopeDesc = new DbSyncScopeDescription("ProductsScope");

        // get the description of the Products table from SyncDB dtabase
        DbSyncTableDescription tableDesc = SqlSyncDescriptionBuilder.GetDescriptionForTable("Products", serverConn);

        // add the table description to the sync scope definition
        scopeDesc.Tables.Add(tableDesc);

        // setting my provision

        // create a server scope provisioning object based on the ProductScope
        SqlSyncScopeProvisioning serverProvision = new SqlSyncScopeProvisioning(serverConn, scopeDesc);

        // skipping the creation of table since table already exists on server
        serverProvision.SetCreateTableDefault(DbSyncCreationOption.Skip);

        // start the provisioning process
        serverProvision.Apply();

这是我的同步代码。

 // create the sync orhcestrator
        SyncOrchestrator syncOrchestrator = new SyncOrchestrator();

        // set local provider of orchestrator to a CE sync provider associated with the 
        // ProductsScope in the SyncCompactDB compact client database
        syncOrchestrator.LocalProvider = new SqlSyncProvider("ProductsScope", clientConn);

        // set the remote provider of orchestrator to a server sync provider associated with
        // the ProductsScope in the SyncDB server database
        syncOrchestrator.RemoteProvider = new SqlSyncProvider("ProductsScope", serverConn);

        // set the direction of sync session to Upload and Download
        syncOrchestrator.Direction = SyncDirectionOrder.UploadAndDownload;

        // subscribe for errors that occur when applying changes to the client
        ((SqlSyncProvider)syncOrchestrator.LocalProvider).ApplyChangeFailed += new EventHandler<DbApplyChangeFailedEventArgs>(Program_ApplyChangeFailed);
        ((SqlSyncProvider)syncOrchestrator.RemoteProvider).ApplyChangeFailed += new EventHandler<DbApplyChangeFailedEventArgs>(Program_ApplyChangeFailed);

        // execute the synchronization process
        SyncOperationStatistics syncStats = syncOrchestrator.Synchronize();

        //


        // this will show me the statistics after sync
        Console.WriteLine("Start Time: " + syncStats.SyncStartTime);
        Console.WriteLine("Total Changes Uploaded: " + syncStats.UploadChangesTotal);
        Console.WriteLine("Total Changes Downloaded: " + syncStats.DownloadChangesTotal);
        Console.WriteLine("Complete Time: " + syncStats.SyncEndTime);
        Console.WriteLine(String.Empty);}

        static void Program_ApplyChangeFailed(object sender, DbApplyChangeFailedEventArgs e)

{

此时,同步是对一行数据的完全重写。我正在寻找的是关于设置同步以识别对各个列所做更改的一些指示(不确定我是否正确地说)。

例如:

Column   ID (PK)    Name        Phone      Address
 Row     1          John Smith  555-5555   123 Anywhere St
 Row     2          Jane Smith  555-5555   124 Anywhere St

销售代表将第1行中的地址更改为125任意位置,销售代表2将第1行中的电话更改为555-5556,有没有办法设置跟踪更改的内容,以便我的最终结果是两位销售代表都有:

Column   ID (PK)  Name        Phone     Address
Row      1        John Smith  555-5556  125 Anywhere St
Row      2        Jane Smith  555-5555  124 Anywhere St
同步完成后

。正如我现在所说的,整个行和重写都是最后一次。导致多个用户对同一记录所做的更改未得到正确处理。

我确信这与设置范围和跟踪的方式有关,但谷歌没有提出任何答案。任何剪辑,链接或建议都会很棒。

1 个答案:

答案 0 :(得分:1)

Sync Framework更改跟踪位于行级别。它没有具体记录哪个列已更改,只记录了插入/更新/删除行的事实。

如果在多个副本中更新了一行,则会导致冲突。您应该能够捕获ApplyChangeFailed事件中的冲突并决定如何解决冲突(例如,服务器获胜,客户获胜,对行进行自定义处理等)。