Vitaly's WebLog
Writing on software development and all related

Microsoft Sync Framework Support in Visual Studio 2008

January 5, 2008

Microsoft Sync Framework is transport agnostic platform for automatic data synchronization across different types of datasources. It can be used for occasionally connected applications (smart clients) and p2p supplications. Previously we had to either use SQL server merge replication or write own synchronization framework. And now we have a solution from Microsoft that looks very promising. There is a good documentation for this on MSDN, if you need more details or code samples you will find them there.

While playing with Visual Studio 2008, I accidentally found new item type called "Local Database Cache". I turned out that this is really Microsoft Sync Framework integration with Visual Studio.

Note: I've tried this with SQL Server 2005. It may work different with SQL Server 2008 because there are new features for tracking data changes.

Microsoft Sync Framework in Visual Studio 2008

When you add this item to your solution, "Configure Data Synchronization" window is opened. There are plenty of settings like server database location, table selection, which column to use to compare etc…

Configure Data Syncronization

Table options

Finally, when you finish configuration, Visual Studio does the following for you:

  • Adds references to necessary assemblies
  • Adds configuration strings for Server DB and for Client DB (SQLCE3.5)
  • Adds SQLScripts and SQLUndo scripts to the solution. There is one script of each type per table. First creates new columns to track updates and inserts (you can select to use existing), triggers to fill update columns, "TombStone" tables (used to track deleted records). Undo scripts can be used to delete all above mentioned.
  • Executes SQL scripts on your SQL Server.
  • Adds client database
  • Thoughtfully creates Dataset for you.
  • Adds "sync" item to the solution. You can open sync configuration by clicking on this item. It also contains autogenerated code.
  • Syncronizes databases for the first time.

After that you should be able to use the following code to synchronize your databases:

// Call SyncAgent.Synchronize() to initiate the synchronization process.

// Synchronization only updates the local database, not your project's data source.

LocalDataCache1SyncAgent syncAgent = new LocalDataCache1SyncAgent();

Microsoft.Synchronization.Data.SyncStatistics syncStats = syncAgent.Synchronize();

// TODO: Reload your project data source from the local database

// (for example, call the TableAdapter.Fill method).

Pretty simple, isn't it?

I've decided to check how it works. To do that I've modified column in one of the tables on the server, and executed the code to sync databases. After that I opened table in local database from Visual Studio. Unfortunately,  my change was not reflected in the local table. Or fortunately, because it forced me to take a closer look on how it works. Running SQL Profiler and debugging the code revealed nothing. Then I decided to check my last suspicion and output local table contents into datagridview. That shown correct results! Syncronization worked just fine! The reason was the caching in visual studio table viewer (I pressed button with red exclamation sight, I swear).


Related posts

Comments

January 5. 2008 08:17

Trackback from DotNetKicks.com

Microsoft Sync Framework Support in Visual Studio 2008

DotNetKicks.com

January 7. 2008 10:55

Pingback from code-inside.de

Wöchentliche Rundablage: LINQ, ASP.NET MVC, Silverlight, WPF, C# 3.0, Oracle (ODAC), SQL Server 2008, Visual Studio 2008… | Code-Inside Blog

code-inside.de

January 9. 2008 14:00

Pingback from buayacorp.com

Enlaces 09/08/2008 en Buayacorp - Diseño y Programación

buayacorp.com

January 13. 2008 01:35

Pingback from blog.aplicacionesweb.cl

Enlaces 09/01/2008 | AplicacionesWeb

blog.aplicacionesweb.cl

January 15. 2008 05:37

Pingback from vincenthomedev.wordpress.com

Microsoft Sync Framework Support in Visual Studio 2008 « vincenthome’s Software Development

vincenthomedev.wordpress.com

February 8. 2008 05:30

Hi

My version of VS2008 does not have the local database cache option. I just downloaded it from the MSDN site. is there a different version?

I have version 3.5.

Adie

February 8. 2008 05:45

Hi Adie,
This feature available not in all editions of VS. You will find it if you have one of the following: Standard Edition, Professional Edition, Team System Architecture Edition, Team System Database Edition, Team System Development Edition, Team System Test Edition, Team System Team Suite

Vitaly Gorn

May 5. 2008 12:43

FYI...once you sync your sdf, it changes the client table to a system table, at which point you cannot edit the schema of the SDF file in VS 2008.

I assume MS is working on it (we'll se how loing it takes).

The only way around is to drop the tables and re-create them (could be intensive dep. on the number of tables)

Vareck

Add comment