locked
Sync Fx - SQL Express 2008 <-> SQL Express 2008 Cannot setup provisioning and continue syncin' RRS feed

  • Question

  • Hello all,

    First i would like to point my first problem. Do i need to provision and deprovision the databases every time before start syncs?

    If not with what way i can tell if there is need of provisioning or not!

    I have created a class so i can manage provisioning and syncing between 2 SQL Express databases, but having difficulty with the exceptions i get.

    OK here is the code.

     

        SqlConnection sourceConn = new SqlConnection(ConfigManager.Config.SourceSyncConnectionString);
        SqlConnection myConn = new SqlConnection(ConfigManager.Config.ConnectionString);
      
      private void InitializeSync()
        {
          #region SET SOURCE PROVIDER
    
          SqlSyncProvider sourceSqlProv = new SqlSyncProvider("MomSync", sourceConn);
    
          try
          {
            DbSyncScopeDescription sourceScope = new DbSyncScopeDescription("MomSync");
    
            DbSyncTableDescription productsSourceTableDesc = SqlSyncDescriptionBuilder.GetDescriptionForTable("Mom_Products", sourceConn);
            DbSyncTableDescription customersSourceTableDesc = SqlSyncDescriptionBuilder.GetDescriptionForTable("Mom_Customers", sourceConn);
            DbSyncTableDescription ordersSourceTableDesc = SqlSyncDescriptionBuilder.GetDescriptionForTable("Mom_Orders", sourceConn);
            DbSyncTableDescription paymentsSourceTableDesc = SqlSyncDescriptionBuilder.GetDescriptionForTable("Mom_Payments", sourceConn);
    
            sourceScope.Tables.Add(productsSourceTableDesc);
            sourceScope.Tables.Add(customersSourceTableDesc);
            sourceScope.Tables.Add(ordersSourceTableDesc);
            sourceScope.Tables.Add(paymentsSourceTableDesc);
    
            SqlSyncScopeProvisioning sourceProvision = new SqlSyncScopeProvisioning(sourceConn, sourceScope);
            sourceProvision.PopulateFromScopeDescription(sourceScope);
            sourceProvision.SetCreateTableDefault(DbSyncCreationOption.Skip);
    
            sourceProvision.Apply();
          }
          catch (Exception ex)
          {
            MessageBox.Show(ex.Message);
          }
    
          #endregion
    
          #region SET MY PROVIDER
    
          SqlSyncProvider myProvider = new SqlSyncProvider("MomClientSync", myConn);
    
          try
          {
            DbSyncScopeDescription scopeSourceDesc = SqlSyncDescriptionBuilder.GetDescriptionForScope("MomSync", sourceConn);
            SqlSyncScopeProvisioning clientProvision = new SqlSyncScopeProvisioning(myConn, scopeSourceDesc);
            clientProvision.PopulateFromScopeDescription(scopeSourceDesc);
            clientProvision.SetCreateTableDefault(DbSyncCreationOption.CreateOrUseExisting);
    
            clientProvision.Apply();
          }
          catch (Exception ex)
          {
            MessageBox.Show(ex.Message);
          }
    
          #endregion
    
          #region SET SYNC ORCHESTRATOR
    
          OrcheStrator = new SyncOrchestrator
                    {
                      LocalProvider = myProvider,
                      RemoteProvider = sourceSqlProv,
                      Direction = SyncDirectionOrder.UploadAndDownload
                    };
    
          ((SqlSyncProvider)OrcheStrator.LocalProvider).ApplyChangeFailed += ApplyChangeFailedHandler;
    
          #endregion
        }
    
        private void ApplyChangeFailedHandler(object sender, DbApplyChangeFailedEventArgs e)
        {
          LogErrors(e.Conflict.Type + " " + e.Error);
        }
    
        public void Synchronize()
        {
          SyncOperationStatistics syncStats = OrcheStrator.Synchronize();
        }
    
        private void DeProvision()
        {
          try
          {
            SqlSyncScopeDeprovisioning sourceDeprovision = new SqlSyncScopeDeprovisioning(sourceConn);
            sourceDeprovision.DeprovisionStore();
    
            SqlSyncScopeDeprovisioning clientDeprovision = new SqlSyncScopeDeprovisioning(myConn);
            clientDeprovision.DeprovisionStore();
          }
          catch {}
        }
    

    Please if anyone, i cant find any proper examples of syncin' between two SQL databases. VS Wizard is great but you can only use it for SQL to SQLCe databases.

    If I leave the code as you see it i get a DbProvisioningException : {"Could not create a scope with name 'MomSync' as a scope with that name already exists."}

    If I try and change the scope name i get DbSyncException : {"Cannot find a valid scope with the name 'MomClientSync' in table '[scope_info]'. Ensure that this scope exists and that it has a corresponding valid configuration in the configuration table '[scope_config]'."}

    I cant get something here. Any help appreciated.

     

    Thanx in advance.

    Thursday, November 18, 2010 4:18 PM

All replies

  • Hi.

    You need to provision the database ONLY ONCE. I assume the exception of "Could not create scope because it already exists" is because you try to provision each time you sync.

     

    Hope this helps.

    Adrian

    Thursday, November 18, 2010 10:25 PM
  • you can use SqSyncScopeProvisioning.ScopeExists(<scopename>) to check if a scope exists before provisioning. 

    also, the Sync Fx documentation that gets installed with the SDK actually contains a step by step tutorial/walkthrough for database synchronization. Take a look at Synchronizing SQL Server and SQL Express under Database Synchronization Scenarios. that should apply to your scenario.

    Friday, November 19, 2010 12:40 AM
  • Will try the tutorial deeply and thank you so far.
    Friday, November 19, 2010 11:34 AM