Sunday, February 17, 2013

Synchronize Tables with Different Table Names - Microsoft Sync Framework

Let’s say you want to sync two tables which have two different names with Microsoft Sync Framework. In my Remote database I have a table named “CUSTOMER” and in my Local database I have two tables named “CUSTOMER” and “NEW_CUSTOMER.” I want table “CUSTOMER” in remote database to be downloaded in to table “NEW_CUSTOMER”.

GlobalName property of DbSyncTableDescription is used for this. When provisioning the server I am setting up the GlobalName property of my table “CUSTOMER” to “NEW_CUSTOMER”.

Here is where I am setting GlobalName to the “Customer” tables’ DbSyncTableDescription when provisioning the server,
SqlConnection serverConn = new SqlConnection("Data Source=.; Initial Catalog=SyncDBServer; Integrated Security=True");
DbSyncScopeDescription scopeDesc = new DbSyncScopeDescription("MySyncScope");
DbSyncTableDescription cusTableDesc = SqlSyncDescriptionBuilder.GetDescriptionForTable("CUSTOMER", serverConn);
cusTableDesc.GlobalName = "NEW_CUSTOMER";
scopeDesc.Tables.Add(cusTableDesc);
There is something important to note when provisioning the client. Most of the times when provisioning the client what we are doing is getting the scope description which we have applied on the server and then applying it on the client. Please remember that, here we can’t do it. We should create the provision scope in the client in the following way.
SqlConnection clientConn = new SqlConnection("Data Source=.; Initial Catalog=SyncDBClient; Integrated Security=True");
DbSyncScopeDescription scopeDesc = new DbSyncScopeDescription("MySyncScope");
DbSyncTableDescription cusTableDesc = SqlSyncDescriptionBuilder.GetDescriptionForTable("NEW_CUSTOMER", clientConn);
scopeDesc.Tables.Add(cusTableDesc);
image
Result
I am uploading a full sample to my sky drive.

Happy Coding.

Regards,
Jaliya