2014-01-07 12 views
0

我想在mvc4網站同步兩個數據庫的SQL?DataSync在Mvc4爲兩個不同的用戶

我有這樣的代碼來同步它顯示沒有錯誤,但沒有得到執行

using Microsoft.Synchronization; 
using Microsoft.Synchronization.Data; 
using Microsoft.Synchronization.Data.SqlServer; 
using System; 
using System.Collections.Generic; 
using System.Data.SqlClient; 
using System.Linq; 
using System.Web; 
using System.Web.Mvc; 

namespace Planetskool.Controllers 
{ 
    public class DataSyncViewController : Controller 
    { 
     // 
     // GET: /DataSyncView/ 

     public ActionResult Index() 
     { 
      string sqlazureConnectionString = "Server=(LocalDb)\v11.0;Initial Catalog=aspnet-Planetskool-20130901224447;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnet-20130901224447.mdf"; 
      string sqllocalConnectionString = "Server=(LocalDb)\v11.0;Initial Catalog=aspnet-Planetskool-20130901224446;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnet-20130901224446.mdf"; 

      using (SqlConnection serverCon = new SqlConnection(sqlazureConnectionString)) 
      using (SqlConnection clientCon = new SqlConnection(sqllocalConnectionString)) 
      { 
       var provider1 = new SqlSyncProvider("scope1", serverCon); 
       var provider2 = new SqlSyncProvider("scope1", clientCon); 

       prepareServer(provider1); 
       prepareClinet(provider2, serverCon); 
       SyncOrchestrator sync = new SyncOrchestrator(); 
       sync.LocalProvider = provider1; 
       sync.RemoteProvider = provider2; 

       sync.Synchronize(); 
       return View(); 

      }} 
      private static void prepareServer(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("Author_Master", connection)); 
      config.PopulateFromScopeDescription(scopeDesc); 
      config.SetCreateTableDefault(DbSyncCreationOption.CreateOrUseExisting); 
      config.Apply(); 
     } 
    } 

    private static void prepareClinet(SqlSyncProvider provider, SqlConnection sourceConnection) 
    { 
     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("Author_Master", sourceConnection)); 
      config.PopulateFromScopeDescription(scopeDesc); 
      config.Apply(); 
     } 
    } 

     } 

    } 
+0

您的初始目錄是相同的,但數據文件不同,是您的意圖? – jlew

+0

@jlew不,它是錯誤的初始目錄也不同,它也沒有執行 –

回答

0

您可以啓用同步框架,詳細模式跟蹤追查同步。同樣,你忘了設置同步方向嗎?

相關問題