我使用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-当用户点击带有“Apply Provision”的按钮时,完成以下操作
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。
P.S:我知道我应该只规定一次范围。
答案 0 :(得分:1)
为什么要跳过存储过程的创建?
remoteProvision.SetCreateProceduresDefault(DbSyncCreationOption.Skip); localProvision.SetCreateProceduresDefault(DbSyncCreationOption.Skip);
如果您知道之前已经配置了表并且已经创建了存储过程,那么您应该只跳过创建。