xplat
xplat

Reputation: 8626

Sync Framework DbProvisioningException

Im trying to learn Sync framework. I have followed step by step MSDN Documentation and it didn't work. Surprised?! What i need is to sync a SQL Express Database with SQL Express Database. While reading about provisioning and other prepare i need to do, i could never find if this must be run every time i want to use sync. I mean, provision -> sync -> deprovision. Another thing is that is that i get this strange exception while making step by step the MSDN sample.

DbProvisioningException "MomScope" already exists...shoudn't be exist?

Code:

private void InitializeSync()
{
    SqlConnection sourceConn = new SqlConnection(ConfigManager.Config.SourceSyncConnectionString);
    SqlConnection myConn = new SqlConnection(ConfigManager.Config.ConnectionString);

    #region SET SOURCE PROVIDER

    SqlSyncProvider sourceSqlProv = new SqlSyncProvider("MomSync", sourceConn);

    DbSyncScopeDescription sourceScope = new DbSyncScopeDescription("MomScope");

    DbSyncTableDescription productsSourceTableDesc = SqlSyncDescriptionBuilder.GetDescriptionForTable("Mom_Products", sourceConn);
    DbSyncTableDescription customersSourceTableDesc = SqlSyncDescriptionBuilder.GetDescriptionForTable("Mom_Customer", 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.SetCreateTableDefault(DbSyncCreationOption.Skip);

    try
    {
        sourceProvision.Apply();
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }

    #endregion

    #region SET MY PROVIDER

    SqlSyncProvider myProvider = new SqlSyncProvider("MomSync", myConn);

    DbSyncScopeDescription scopeSourceDesc = SqlSyncDescriptionBuilder.GetDescriptionForScope("MomScope", sourceConn); <===== DbProvisioningException (MomScope already exists????)
    SqlSyncScopeProvisioning myProvision = new SqlSyncScopeProvisioning(myConn, scopeSourceDesc);

    try
    {
        myProvision.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
}

Any proper sync samples? So i can figure properly how to create this task?

Thank you.

Upvotes: 1

Views: 2149

Answers (2)

You need not provision everytime before sync. Provision can be one time and then you can sync from there on. Ofcourse if de-provision happens at time ..sycn canot be done after that.

In your case, as the message suggests, there seems to be existing already with that same name. Check by running following statement

select * from scope_info

If it returns rows with scope name which you are trying to add, you can do following things.

  1. De-provision the DBs for that Scope. But you need to make sure that scope is not in use by anyone else.

(OR)

  1. Provision the DBs with a different name

Upvotes: 0

stombeur
stombeur

Reputation: 2714

Should anyone still be interested...

Just implement a check on sourceProvision.Exists() right before you apply() http://msdn.microsoft.com/en-us/library/dd919024.aspx

Upvotes: 2

Related Questions