C# 使用同步框架2.1 DbSyncException将SQL CE与SQL Server同步
我尝试制作一个支持脱机使用的应用程序。我认为微软同步框架是个好主意 但我尝试在我的SQL server数据库和SQL CE数据库中只使用一个表来制作一个小演示:C# 使用同步框架2.1 DbSyncException将SQL CE与SQL Server同步,c#,sql,sql-server,database,synchronization,C#,Sql,Sql Server,Database,Synchronization,我尝试制作一个支持脱机使用的应用程序。我认为微软同步框架是个好主意 但我尝试在我的SQL server数据库和SQL CE数据库中只使用一个表来制作一个小演示: CREATE TABLE [Products] ( [ID] int IDENTITY (1,1) NOT NULL , [Name] nvarchar(50) NOT NULL , [ListPrice] money NOT NULL ); GO ALTER TABLE [Products] ADD CONSTRAINT [PK
CREATE TABLE [Products] (
[ID] int IDENTITY (1,1) NOT NULL
, [Name] nvarchar(50) NOT NULL
, [ListPrice] money NOT NULL
);
GO
ALTER TABLE [Products] ADD CONSTRAINT [PK_Products] PRIMARY KEY ([ID]);
GO
在服务器和CE上创建这些表后,我想尝试同步。以下是我为同步这两个数据库而编写的代码:
public void Synchronize()
{
using (var serverConnection = new SqlConnection(_connectionStringServer))
using (var clientConnection = new SqlCeConnection(_connectionStringClient))
{
var provider1 = new SqlSyncProvider("scopeTest", serverConnection);
var provider2 = new SqlCeSyncProvider("scopeTest", clientConnection);
//config
PrepareServerForProvisioning(provider1,"Products", "ProductTable");
PrepareClientForProvisioning(provider2, serverConnection, "Products");
//sync
SyncOrchestrator sync = new SyncOrchestrator();
sync.LocalProvider = provider1;
sync.RemoteProvider = provider2;
sync.Direction = SyncDirectionOrder.UploadAndDownload;
sync.Synchronize();
Console.Read();
}
}
private static void PrepareServerForProvisioning(SqlSyncProvider provider, string description, string table)
{
SqlConnection connection = (SqlConnection)provider.Connection;
SqlSyncScopeProvisioning config = new SqlSyncScopeProvisioning(connection);
if (!config.ScopeExists(provider.ScopeName))
{
DbSyncScopeDescription scopeDesc = new DbSyncScopeDescription(provider.ScopeName);
scopeDesc.Tables.Add(SqlSyncDescriptionBuilder.GetDescriptionForTable(description, connection));
DbSyncTableDescription serverTableDesc = SqlSyncDescriptionBuilder.GetDescriptionForTable(description, connection);
serverTableDesc.GlobalName = table;
config.PopulateFromScopeDescription(scopeDesc);
config.SetCreateTableDefault(DbSyncCreationOption.CreateOrUseExisting);
config.Apply();
}
}
private void PrepareClientForProvisioning(SqlCeSyncProvider provider, SqlConnection serverConnection, string tablename)
{
// create a connection to the SyncCompactDB database
SqlCeConnection clientConn = (SqlCeConnection)provider.Connection;
// get the description of ProductsScope from the SyncDB server database
DbSyncScopeDescription scopeDesc = SqlSyncDescriptionBuilder.GetDescriptionForScope("scopeTest", serverConnection);
// create CE provisioning object based on the ProductsScope
SqlCeSyncScopeProvisioning clientProvision = new SqlCeSyncScopeProvisioning(clientConn, scopeDesc);
if (!clientProvision.ScopeExists(provider.ScopeName))
{
// starts the provisioning process
clientProvision.Apply();
}
}
在运行“PrepareServerForProvisioning”方法后,服务器数据库创建了表:Products\u tracking、Schema\u info、Scope\u config和Scope\u info,连接字符串工作正常,服务器端的资源调配工作正常
当代码到达以下行时:sync.Synchronize();它抛出一个错误:
An unhandled exception of type 'Microsoft.Synchronization.Data.DbSyncException' occurred in Microsoft.Synchronization.dll
Additional information: An error occurred when initializing adapters for scope 'scopeTest'. Ensure that the scope name specified on the provider exists in the scope metadata table, and that the correct base tables are specified for that scope. Check the inner exception for any store-specific errors.
innerexception如下所示:
{“无法将SyncAdapter添加到SyncAdapterCollection。表'Products'的SyncAdapter已存在。”}
我做错了什么