如何在2个不同的数据库表中同步不同列的名称?

时间:2014-04-11 07:46:05

标签: c# azure azure-sql-database azure-storage microsoft-sync-framework

您好我想同步2个不同的数据库和表格和columns.i创建了SYNC应用程序。通过使用同步framework.it由azure托管。我也读过以下文章:

http://jtabadero.wordpress.com/2011/08/19/part-4-synchronizing-tables-with-different-table-names-and-column-names/ http://www.devart.com/dotconnect/oracle/docs/SyncFramework.html
但我有一个Nullreference例外。 OrderTable没有正确的列。看图片。如何解决这个问题。

namespace WorkerRole1
{
    public class WorkerRole : RoleEntryPoint
    {
        public override void Run()
        {
            // This is a sample worker implementation. Replace with your logic.
            Trace.TraceInformation("WorkerRole1 entry point called", "Information");
            Setup();
            while (true)
            {
                Sync();
                Thread.Sleep(10000);
                Trace.TraceInformation("Working", "Information");
            }
        }

    private void Setup()
    {

        string scopeName = "DifferentSchemaScope";
        string MemberSQLAzureConnectionString = ConfigurationManager.ConnectionStrings["MemberSQLAzureConnectionString"].ConnectionString;
        string HubSQLAzureConnectionString = ConfigurationManager.ConnectionStrings["HubSQLAzureConnectionString"].ConnectionString;
        using (SqlConnection sqlMemberAzureConn = new SqlConnection(MemberSQLAzureConnectionString))
        {

            using (SqlConnection sqlHubAzureConn = new SqlConnection(HubSQLAzureConnectionString))
            {


                if (sqlHubAzureConn.State == System.Data.ConnectionState.Open && sqlMemberAzureConn.State == ConnectionState.Open)
                {


                    DbSyncScopeDescription myScope = new DbSyncScopeDescription(scopeName);

                    DbSyncTableDescription serverTableDesc = SqlSyncDescriptionBuilder.GetDescriptionForTable("myTest.SourceOrderTable", sqlHubAzureConn);
                    serverTableDesc.GlobalName = "OrderTable";

                    DbSyncTableDescription clientTableDesc = SqlSyncDescriptionBuilder.GetDescriptionForTable("myTest.DestinationOrderTable", sqlMemberAzureConn);


                    clientTableDesc.GlobalName = "OrderTable";

                    myScope.Tables.Add(serverTableDesc);
                    myScope.Tables.Add(clientTableDesc);




                    SqlSyncScopeProvisioning sqlServerProv = new SqlSyncScopeProvisioning(myScope);
                    SqlSyncScopeProvisioning sqlAzureProv = new SqlSyncScopeProvisioning(myScope);

                    sqlServerProv.PopulateFromScopeDescription(myScope);




                    myScope.Tables["OrderTable"].Columns.Remove(myScope.Tables["OrderTable"].Columns["OrderQty"]);
                    myScope.Tables["OrderTable"].Columns["OrderId"].IsPrimaryKey = true;
                    sqlAzureProv.PopulateFromScopeDescription(myScope);



                    if (!sqlServerProv.ScopeExists(scopeName, sqlHubAzureConn))
                    {

                        sqlServerProv.Apply(sqlHubAzureConn);

                    }

                    // sqlAzureProv.SetCreateTableDefault(DbSyncCreationOption.Skip);

                    if (!sqlAzureProv.ScopeExists(scopeName, sqlMemberAzureConn))
                    {

                        sqlAzureProv.Apply(sqlMemberAzureConn);
                    }
                }
            }
        }
    }


    private void Sync()
    {

        string scopeName = "DifferentSchemaScope";
        string MemberSQLAzureConnectionString = ConfigurationManager.ConnectionStrings["MemberSQLAzureConnectionString"].ConnectionString; //CloudConfigurationManager.GetSetting("MemberSQLAzureConnectionString");
        string HubSQLAzureConnectionString = ConfigurationManager.ConnectionStrings["HubSQLAzureConnectionString"].ConnectionString;  //CloudConfigurationManager.GetSetting("HubSQLAzureConnectionString");
        using (SqlConnection sqlMemberAzureConn = new SqlConnection(MemberSQLAzureConnectionString))
        {

            using (SqlConnection sqlHubAzureConn = new SqlConnection(HubSQLAzureConnectionString))
            {

                var localProvider = new SqlSyncProvider(scopeName, sqlHubAzureConn);
                var remoteProvider = new SqlSyncProvider(scopeName, sqlMemberAzureConn);

                remoteProvider.ChangesSelected += remoteProvider_ChangesSelected;

                SyncOrchestrator syncOrchestrator = new SyncOrchestrator
                {
                    LocalProvider = localProvider,
                    RemoteProvider = remoteProvider,
                    Direction = SyncDirectionOrder.UploadAndDownload
                };

                syncOrchestrator.Synchronize();
            }
        }
    }



    void remoteProvider_ChangesSelected(object sender, DbChangesSelectedEventArgs e)
    {
        if (e.Context.DataSet.Tables.Contains("OrderTable"))
        {
            DataTable dataTable = new DataTable();
            dataTable = e.Context.DataSet.Tables["OrderTable"];

            //rename the columns to match the destination table’s column names
            dataTable.Columns["OrderId"].ColumnName = "OrderNo";
            dataTable.Columns["OrderDesc"].ColumnName = "OrderDetail";

        }
    }

    public override bool OnStart()
    {
        // Set the maximum number of concurrent connections 
        ServicePointManager.DefaultConnectionLimit = 12;

        // For information on handling configuration changes
        // see the MSDN topic at http://go.microsoft.com/fwlink/?LinkId=166357.

        return base.OnStart();
    }
}

}

SQL:

id应该是主键....

CREATE SCHEMA myTest

CREATE TABLE [myTest]。[SourceOrderTable](     [OrderId] [int] IDENTITY(1,1)NOT NULL,     [OrderDesc] nvarchar NULL)

CREATE TABLE [myTest].[DestinationOrderTable](
[OrderNo] [int] IDENTITY(1,1) NOT NULL,
[OrderDetail] [nvarchar](50) NULL,
[OrderQty] int NULL)

但它不起作用。它产生了一个错误“void remoteProvider_ChangesSelected(object sender,DbChangesSelectedEventArgs e)”这就是为什么remoteprovider列名没有改变.remoteProvider列名称是“OrderNo,OrderDetail”但它必须是“OrderId,OrderDesc”看起来图片:

enter image description here

错误图片:

enter image description here

1 个答案:

答案 0 :(得分:2)

检查您的同步方向。 它表示上传和下载,您将远程提供程序上的ChangesSelected绑定到您的目标位置。

该事件将在下载时触发​​,您的“目的地”现在是您的来源。所以数据集中的数据是有效的,这就是目标表中选择更改的结构(现在是源,因为下载时同步方向相反)

由于您正在进行双向同步,因此您应该在两个提供程序上都有ChangesSelected事件。

本地提供商ChangesSelected应映射OrderId - > OrderNo / OrderDesc-> OrderDetail

远程提供商ChangesSelected应该是相反的OrderNo - > OrderId / OrderDetail-> OrderDesc