Safi Mustafa
Safi Mustafa

Reputation: 517

Sync Framework not Provisiong Sql Compact when number of primary keys are different

I am trying to sync Sql Compact With Sql Server. Every thing works fine if number of primary keys are similar in client and server table. But If number of primary keys are not same in both tables I Get the following Exception

The given key was not present in the dictionary.

Stack Trace:

at System.ThrowHelper.ThrowKeyNotFoundException() at System.Collections.Generic.Dictionary2.get_Item(TKey key) at Microsoft.Synchronization.Data.SqlServerCe.SqlCeSyncUtil.GetPkInOrdinalOrder(IEnumerable1 pkColumns, Dictionary`2 pkIndexOrder) at Microsoft.Synchronization.Data.SqlServerCe.SqlCeSyncTableProvisioning.Apply(SqlCeTransaction trans) at Microsoft.Synchronization.Data.SqlServerCe.SqlCeSyncScopeProvisioning.ApplyInternal(SqlCeConnection connection) at Microsoft.Synchronization.Data.SqlServerCe.SqlCeSyncScopeProvisioning.Apply() at SQLCompactDeployment.Program.ProvisionClient(String ServerConnectionString, String ClientConnectionString, Boolean IsSqlServerCompact) in M:\Windows 2020\Documents\repos\SQLCompactDeployment\SQLCompactDeployment\Program.cs:line 296

Table Structure in Server

USE [Server]
GO

/****** Object:  Table [dbo].[Customer]    Script Date: 3/26/2020 6:09:31 PM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[Customer](
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [Name] [varchar](150) NULL,
    [TenantId] [uniqueidentifier] NOT NULL,
 CONSTRAINT [PK_Customer_1] PRIMARY KEY CLUSTERED 
(
    [Id] ASC,
    [TenantId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO

Table Structure In Client

USE [Client]
GO

/****** Object:  Table [dbo].[Customer]    Script Date: 3/26/2020 6:09:20 PM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[Customer](
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [Name] [varchar](150) NULL,
    [TenantId] [uniqueidentifier] NOT NULL,
 CONSTRAINT [PK_Customer_1] PRIMARY KEY CLUSTERED 
(
    [Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO

Syncing Code

public static bool ProvisionServer(string ServerConnectionString)
{
    try
    {
        string scopeName = "SyncScope";
        SqlConnection serverConn = new SqlConnection(ServerConnectionString);
        DbSyncScopeDescription SyncScope = new DbSyncScopeDescription(scopeName);
        DbSyncTableDescription Customer = SqlSyncDescriptionBuilder.GetDescriptionForTable("Customer", serverConn);
        SyncScope.Tables.Add(Customer);
        SqlSyncScopeProvisioning serverProvision = new SqlSyncScopeProvisioning(serverConn, SyncScope);
        serverProvision.SetCreateTableDefault(DbSyncCreationOption.Skip);
        serverProvision.Apply();
        return true;
    }
    catch (Exception ex)
    {
        return false;
    }
}

public static bool ProvisionClient(string ServerConnectionString, string ClientConnectionString, bool IsSqlServerCompact)
{
    try
    {
        string scopeName = "SyncScope";
        SqlConnection serverConn = new SqlConnection(ServerConnectionString);
        DbSyncScopeDescription scopeDesc = SqlSyncDescriptionBuilder.GetDescriptionForScope(scopeName, serverConn);
        if (IsSqlServerCompact)
        {
            SqlCeConnection clientConnCe = new SqlCeConnection(ClientConnectionString);
            SqlCeSyncScopeProvisioning clientProvision = new SqlCeSyncScopeProvisioning(clientConnCe, scopeDesc);
            clientProvision.Apply();
        }
        else
        {
            SqlConnection clientConn = clientConn = new SqlConnection(ClientConnectionString);
            SqlSyncScopeProvisioning clientProvision = new SqlSyncScopeProvisioning(clientConn, scopeDesc);
            clientProvision.Apply();
        }
        return true;
    }
    catch (Exception ex)
    {
        return false;
    }

}

With the same structure If I sync From Sql server database to Sql server database it Works.

Upvotes: 2

Views: 112

Answers (0)

Related Questions