我使用 Sync 框架编写了一个包装器来提供通用功能。
这是我的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Synchronization.Data;
using Microsoft.Synchronization.Data.SqlServer;
using System.Data.SqlClient;
using Microsoft.Synchronization;
namespace PluginTest.Components
{
public class P2PProvsionProvider
{
public SqlConnection RemoteConnection { get; set; }
public SqlConnection LocalConnection { get; set; }
private static Dictionary<string,DbSyncScopeDescription> scopes { get; set; }
private static SqlSyncScopeProvisioning remoteProvision { get; set; }
private static SqlSyncScopeProvisioning localProvision { get; set; }
private static P2PProvsionProvider provider;
//private P2PProvsionProvider() {
//}
public P2PProvsionProvider() {
scopes = new Dictionary<string, DbSyncScopeDescription>();
}
public static P2PProvsionProvider getInstance(string name) {
if (name.Equals("default"))
if (provider == null)
provider = new P2PProvsionProvider();
return provider;
}
//1
public void CreateProvisionScope(string name) {
if (!scopes.ContainsKey(name))
{
scopes.Add(name, new DbSyncScopeDescription(name));
}
}
//2
public void AddProvisionTable(string scopeName, string tableName) {
if (scopes[scopeName] != null)
{
DbSyncScopeDescription remoteScopeDesc = scopes[scopeName];
//DbSyncScopeDescription localScopeDesc = scopes[scopeName];
DbSyncTableDescription remoteTableDesc = SqlSyncDescriptionBuilder.GetDescriptionForTable(tableName, RemoteConnection);
//DbSyncTableDescription localTableDesc = SqlSyncDescriptionBuilder.GetDescriptionForTable(tableName, LocalConnection);
remoteScopeDesc.Tables.Add(remoteTableDesc);
//localScopeDesc.Tables.Add(localTableDesc);
}
}
//3
public void ApplyScopeProvisioning (string scopeName){
remoteProvision = new SqlSyncScopeProvisioning(RemoteConnection, scopes[scopeName]);
remoteProvision.SetCreateProceduresDefault(DbSyncCreationOption.Skip);
remoteProvision.Apply();
localProvision = new SqlSyncScopeProvisioning(LocalConnection, scopes[scopeName]);
localProvision.SetCreateProceduresDefault(DbSyncCreationOption.Skip);
localProvision.Apply();
}
#region Scope Provisioning Builder methods
public SqlSyncScopeProvisioning BuilderCreateProcedures(DbSyncCreationOption option)
{
remoteProvision.SetCreateProceduresDefault(option);
return remoteProvision;
}
public SqlSyncScopeProvisioning BuilderCreateProceduresForAdditionalScope(DbSyncCreationOption option)
{
remoteProvision.SetCreateProceduresForAdditionalScopeDefault(option);
return remoteProvision;
}
public SqlSyncScopeProvisioning BuilderCreateTable(DbSyncCreationOption option)
{
remoteProvision.SetCreateTableDefault(option);
return remoteProvision;
}
public SqlSyncScopeProvisioning BuilderCreateTrackingTable(DbSyncCreationOption option)
{
remoteProvision.SetCreateTrackingTableDefault(option);
return remoteProvision;
}
public SqlSyncScopeProvisioning BuilderCreateTriggers(DbSyncCreationOption option)
{
remoteProvision.SetCreateTriggersDefault(option);
return remoteProvision;
}
public SqlSyncScopeProvisioning BuilderPopulateTrackingTable(DbSyncCreationOption option)
{
remoteProvision.SetPopulateTrackingTableDefault(option);
return remoteProvision;
}
public SqlSyncScopeProvisioning BuilderUseBulkProcedures(bool useBulk)
{
remoteProvision.SetUseBulkProceduresDefault(useBulk);
return remoteProvision;
}
#endregion
//4
public SyncOperationStatistics ExecuteSync(string scopeName , SyncDirectionOrder direction = SyncDirectionOrder.UploadAndDownload) {
SyncOrchestrator syncOrachestrator = new SyncOrchestrator() {
LocalProvider = new SqlSyncProvider(scopeName, LocalConnection),
RemoteProvider = new SqlSyncProvider(scopeName, RemoteConnection),
Direction = direction
};
((SqlSyncProvider)syncOrachestrator.LocalProvider).ApplyChangeFailed += (s,e) =>
{
};
return syncOrachestrator.Synchronize();
}
//5 Deprovision certain scope
public void DeprovisionScope(SqlConnection conn, string scopeName) {
SqlSyncScopeDeprovisioning deprovisionScope = new SqlSyncScopeDeprovisioning(conn);
deprovisionScope.DeprovisionScope(scopeName);
}
//6 Deprovision All
public void DeprovisionStore(SqlConnection conn)
{
SqlSyncScopeDeprovisioning deprovisionScope = new SqlSyncScopeDeprovisioning(conn);
deprovisionScope.DeprovisionStore();
}
}
}
正如问题所暗示的那样,我收到一个 InvalidOperationException 说,说某些存储过程不存在。该应用程序的工作方式如下:在表单中提供以下定义
public GlobalFramework.Core core { get; set; }
public BindingList<string> addedTables { get; set; }
public BindingList<string> provisionedTables { get; set; }
public P2PProvsionProvider provider { get; set; }
public SyncOperationStatistics stats { get; set; }
1-当用户单击带有“添加供应范围”文本的按钮时,将完成以下操作:
private void provisionButton_Click(object sender, EventArgs e)
{
provider.CreateProvisionScope(provisionScopeTextBox.Text);
foreach (var item in provisionedTables)
{
provider.AddProvisionTable(provisionScopeTextBox.Text, item);
}
}
2-当用户单击带有“应用配置”的按钮时,完成以下操作
private void applyProvisionButton_Click(object sender, EventArgs e)
{
provider.ApplyScopeProvisioning(provisionScopeTextBox.Text);
}
3-当用户单击“同步”按钮时,完成以下操作
private void syncButton_Click(object sender, EventArgs e)
{
stats = provider.ExecuteSync(provisionScopeTextBox.Text);
statsLabel.Text = "Start time: " + stats.SyncStartTime + "End Time: " +stats.SyncEndTime+ "Change Total: "+stats.DownloadChangesTotal;
}
现在我想知道为什么我不断收到这个异常?我的本地数据库连接是 SQL Server 2008 R2。我的远程数据库也是 SQL Server 2008 R2。
PS:我知道我应该只配置一次范围。