In this small projects I faced couple of problems.
- An error in SSMS 2008 R2.
- Synchronize error [Database is not provisioned].
- Object Schema problem.
Problem 1:
This problem occurred because of database version difference. My local machine Database is SQL Server 2008 r2 and plesk panel database is SQL Server 2005.
SOLUTION:
Updated my local machine database to service pack 2.
Problem 2 & Problem 3
Problem 2 and 3 are related. In sql server 2008 the default object schema is "dbo" [ex. dbo.tablename] and in sql server 2005 default object is "[DATABASENAME]" [ex. TestSync.tablename considering database name is TestSync]. That's why the sync error occurred.
So to fix the problem I tried to set the object schema manually from within the code and set `config.ObjectSchema = "dbo";` in client provisioning.
But surprisingly it didn't work at all. The plesk panel server database was still creating its default object schema [TestSync.tablename]. When I tried debugging the code I learned that if I set the object schema to "dbo" it takes a null value and really don't know the reason for that.
SOLUTION
So I set the object schema to ".dbo".
config.ObjectSchema = ".dbo";
And now it works perfectly. Here is the complete working code...
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using Microsoft.Synchronization;
using Microsoft.Synchronization.Data;
using Microsoft.Synchronization.Data.SqlServer;
using Microsoft.Synchronization.Data.SqlServerCe;
namespace ExecuteExpressSync
{
class Program
{
static void Main(string[] args)
{
SqlConnection clientConn = new SqlConnection("Data Source=208.91.198.174; Initial Catalog=RANAJOYDADIGI;Integrated Security=no;User ID=RANAJOYDADIGI;Password=RANAJOYDADIGI;Encrypt=no;Integrated Security=No");
SqlConnection serverConn = new SqlConnection("Data Source=(local); Initial Catalog=ANIRUDDHADHARTEST; Integrated Security=True");
var providerl = new SqlSyncProvider("scopel", serverConn);
var provider2 = new SqlSyncProvider("scopel", clientConn);
// corvfig
PrepareServerForProvisioning(providerl);
PrepareClientForProvisioning(provider2, serverConn);
//sync
SyncOrchestrator sync = new SyncOrchestrator();
sync.LocalProvider = providerl;
sync.RemoteProvider = provider2;
SyncOperationStatistics syncStats = sync.Synchronize();
//Deprovision
deprovisionServerClient(providerl);
deprovisionServerClient(provider2);
serverConn.Close();
serverConn.Dispose();
clientConn.Close();
clientConn.Dispose();
}
private static void PrepareServerForProvisioning(SqlSyncProvider provider)
{
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("dbo.Products", connection));
scopeDesc.Tables.Add(SqlSyncDescriptionBuilder.GetDescriptionForTable("dbo.Orders", connection));
config.PopulateFromScopeDescription(scopeDesc);
config.SetCreateTableDefault(DbSyncCreationOption.CreateOrUseExisting);
config.Apply();
}
}
private static void PrepareClientForProvisioning(SqlSyncProvider provider, SqlConnection sourceConnection)
{
SqlSyncScopeProvisioning config = new SqlSyncScopeProvisioning((SqlConnection)provider.Connection);
config.ObjectSchema = ".dbo";
if (!config.ScopeExists(provider.ScopeName))
{
DbSyncScopeDescription scopeDesc = SqlSyncDescriptionBuilder.GetDescriptionForScope(provider.ScopeName, sourceConnection);
config.PopulateFromScopeDescription(scopeDesc);
config.Apply();
}
}
private static void deprovisionServerClient(SqlSyncProvider provider)
{
SqlSyncScopeDeprovisioning serverSqlDepro = new SqlSyncScopeDeprovisioning((SqlConnection)provider.Connection);
serverSqlDepro.DeprovisionScope(provider.ScopeName);
serverSqlDepro.DeprovisionStore();
}
}
}
Finally thank you everyone for the help you have provided.