Question

I am using synchronization framework to sync two databases. Standard procedure to create sync designer file is, select Tables using Data source configuration wizard. I created .sync file and SDF file. However an exception is happen in following code when I synchronize with target database:

var syncDataServerSyncProvider =
new SyncDataServerSyncProvider(
ConfigurationManager.ConnectionStrings["ConnectionString"].
            ConnectionString);
var syncAgent = new SyncDataSyncAgent();
syncAgent.LocalProvider = syncDataClientSyncProvider;
syncAgent.RemoteProvider = syncDataServerSyncProvider;

syncDataClientSyncProvider.SyncProgress += new    EventHandler<SyncProgressEventArgs>(ShowClientProgress);
syncDataClientSyncProvider.ApplyChangeFailed += new EventHandler<ApplyChangeFailedEventArgs>(ShowFailures);

SyncStatistics syncStats = syncAgent.Synchronize();//Exception happen here

The Exception is:

System.Data.SqlClient.SqlException :Unable to initialize the client database, because the schema for table "tables name" could not be retrieved by the GetSchema() method of DbServerSyncProvider

The Inner Exception is:

Change tracking is not enabled on table "table Name"

This exception can be solved by selecting a table to the sync designer from target database using Data source configuration wizard. We can enable change tracking in this wizard. But in reality, in deployment scenario we cant use designer file as we cant use Visual studio.

So my questions are

  1. Why this object tracking must be enable?
  2. How we can enable this object tracking in C# code without depending on Designer?
Was it helpful?

Solution

In order to perform synchronization, MSF needs to track changes (updates / deletes / inserts) in the databases. Without knowing these changes, synchronization wouldn't be possible.

Note that change tracking can be coupled or decoupled :

Coupled change tracking means that change-tracking metadata for inserts and updates is stored in the base table, with a tombstone table to track deletes. Decoupled change tracking means that metadata for inserts, updates, and deletes is stored in a separate table (typically one table for each base table)

With either kind of change tracking, the commands that you specify for the DbSyncAdapter object use change tracking metadata to determine the incremental changes that have been made at each peer.

Here you are using the coupled change tracking option included in SQL server (available since SQL server 2008). So as you said, you must enable change tracking in the database:

  • first at database level
  • then at table level (for all tables that need to be synchronized)

This can be done with SQL Server Management Studio (Database properties > Change Tracking or Table properties > Change Tracking) or with scripts:

For the database:

ALTER DATABASE [DatabaseName] SET CHANGE_TRACKING = ON 

For the table:

ALTER TABLE [TableName] ENABLE CHANGE_TRACKING 

A important thing that you need to customize according to your need is the retention period of these changes (default is 2 days):

The change retention value specifies the time period for which change tracking information is kept. Change tracking information that is older than this time period is removed periodically. When you are setting this value, you should consider how often applications will synchronize with the tables in the database. The specified retention period must be at least as long as the maximum time period between synchronizations. If an application obtains changes at longer intervals, the results that are returned might be incorrect because some of the change information has probably been removed.

Sample query with custom period and disable auto-cleanup:

ALTER DATABASE [TableName] 
  SET CHANGE_TRACKING = ON (CHANGE_RETENTION = 90 MINUTES, AUTO_CLEANUP = OFF)

And useful link from the Sql Server documentation Enable and Disable Change Tracking (SQL Server)

OTHER TIPS

Problem is solved now.I have enabled object tracking in SQL server by running following queries

Alter database 'DATABASENAME'
set CHANGE_TRACKING =ON

go

ALTER TABLE 'TableName'
ENABLE CHANGE_TRACKING

But my first question is still remaining.Why this object tracking is imporatant for syncronisation framework?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top