Microsoft Sync Framework:一个字段 fk 另一个描述
Microsoft Sync Framework: one field fk the other description
我正在尝试使用 Microsoft Sync Framework 来同步 2 tables。我正在使用 SyncOrchestrator 同步两个 tables.
问题是 Employees 有一个字段 cate_id,它有类别的 ID(这是类别的 FK),而 Employees2 有一个名为 Cat 的字段,它将类别的名称存储在字段直接如此(不是 fk),如果你看到一个 table 有 id 另一个名字,
如何同步那些 table?有什么建议吗?
这是我的代码
static void Main(string[] args)
{
//setup the connections
var serverConn = new SqlConnection(@"Data Source=WIN-R9D162FO6E3\HCNSQL07;User ID=mauricio;Password=Maitolin26; Initial Catalog=test;");
var clientConn = new SqlConnection(@"Data Source=WIN-R9D162FO6E3\ESP;User ID=sa;Password=Maitolin26#; Initial Catalog=MedicalDirector;");
//setup scope name
const string scopeName = "DifferentSchemaScope";
//IEnumerable<string> tablesThatChanged
//IEnumerable<string> tablesThatChanged = Enumerable.Empty<string>();
IEnumerable<string> tablesThatChanged = new string[] { "BCPeDoctors" };
//reprovision
//ReProvision(serverConn, scopeName, tablesThatChanged);
//provision server
var serverProvision = new SqlSyncScopeProvisioning(serverConn);
if (!serverProvision.ScopeExists(scopeName))
{
var serverScopeDesc = new DbSyncScopeDescription(scopeName);
// add the server table
var serverTableDesc = SqlSyncDescriptionBuilder.GetDescriptionForTable("dbo.BCPeDoctor", serverConn);
serverTableDesc.GlobalName = "BCPeDoctors";
// removing columns the source doesnt have
serverScopeDesc.Tables["BCPeDoctors"].Columns.Remove(serverScopeDesc.Tables["BCPeDoctors"].Columns["SuburbID"]);
serverScopeDesc.Tables["BCPeDoctors"].Columns.Remove(serverScopeDesc.Tables["BCPeDoctors"].Columns["Location"]);
serverScopeDesc.Tables["BCPeDoctors"].Columns.Remove(serverScopeDesc.Tables["BCPeDoctors"].Columns["SpecialtyID"]);
serverScopeDesc.Tables["BCPeDoctors"].Columns.Remove(serverScopeDesc.Tables["BCPeDoctors"].Columns["Adjusted"]);
serverScopeDesc.Tables["BCPeDoctors"].Columns.Remove(serverScopeDesc.Tables["BCPeDoctors"].Columns["Deleted"]);
serverScopeDesc.Tables["BCPeDoctors"].Columns.Remove(serverScopeDesc.Tables["BCPeDoctors"].Columns["InActive"]);
serverScopeDesc.Tables["BCPeDoctors"].Columns.Remove(serverScopeDesc.Tables["BCPeDoctors"].Columns["IsPreferred"]);
// our server and client tables has different names, so let’s setup a common name
serverScopeDesc.Tables["BCPeDoctors"].Columns["DoctorID"].IsPrimaryKey = true;
serverScopeDesc.Tables.Add(serverTableDesc);
serverProvision.PopulateFromScopeDescription(serverScopeDesc);
//apply the scope definition
serverProvision.Apply();
}
//provision client
var clientProvision = new SqlSyncScopeProvisioning(clientConn);
if (!clientProvision.ScopeExists(scopeName))
{
var clientScopeDesc = new DbSyncScopeDescription(scopeName);
// add the client table
var clientTableDesc = SqlSyncDescriptionBuilder.GetDescriptionForTable("CM_ADDRESS_BOOK", clientConn);
clientTableDesc.GlobalName = "CM_ADDRESS_BOOK2";
clientScopeDesc.Tables.Add(clientTableDesc);
clientScopeDesc.Tables["CM_ADDRESS_BOOK"].Columns["ADDRESS_BOOK_ID"].IsPrimaryKey = true;
// remove the columns the Source table doesnt have it
clientScopeDesc.Tables["CM_ADDRESS_BOOK"].Columns.Remove(clientScopeDesc.Tables["CM_ADDRESS_BOOK"].Columns["ADDRESS_BOOK_NO"]);
clientScopeDesc.Tables["CM_ADDRESS_BOOK"].Columns.Remove(clientScopeDesc.Tables["CM_ADDRESS_BOOK"].Columns["STREET_LINE_3"]);
clientScopeDesc.Tables["CM_ADDRESS_BOOK"].Columns.Remove(clientScopeDesc.Tables["CM_ADDRESS_BOOK"].Columns["CITY"]);
clientScopeDesc.Tables["CM_ADDRESS_BOOK"].Columns.Remove(clientScopeDesc.Tables["CM_ADDRESS_BOOK"].Columns["POSTCODE"]);
clientScopeDesc.Tables["CM_ADDRESS_BOOK"].Columns.Remove(clientScopeDesc.Tables["CM_ADDRESS_BOOK"].Columns["P_STREET_LINE_1"]);
clientScopeDesc.Tables["CM_ADDRESS_BOOK"].Columns.Remove(clientScopeDesc.Tables["CM_ADDRESS_BOOK"].Columns["P_STREET_LINE_2"]);
clientScopeDesc.Tables["CM_ADDRESS_BOOK"].Columns.Remove(clientScopeDesc.Tables["CM_ADDRESS_BOOK"].Columns["P_STREET_LINE_3"]);
clientScopeDesc.Tables["CM_ADDRESS_BOOK"].Columns.Remove(clientScopeDesc.Tables["CM_ADDRESS_BOOK"].Columns["P_CITY"]);
clientScopeDesc.Tables["CM_ADDRESS_BOOK"].Columns.Remove(clientScopeDesc.Tables["CM_ADDRESS_BOOK"].Columns["P_POSTCODE"]);
clientScopeDesc.Tables["CM_ADDRESS_BOOK"].Columns.Remove(clientScopeDesc.Tables["CM_ADDRESS_BOOK"].Columns["PHONE_WORK"]);
clientScopeDesc.Tables["CM_ADDRESS_BOOK"].Columns.Remove(clientScopeDesc.Tables["CM_ADDRESS_BOOK"].Columns["PAGER"]);
clientScopeDesc.Tables["CM_ADDRESS_BOOK"].Columns.Remove(clientScopeDesc.Tables["CM_ADDRESS_BOOK"].Columns["PROVIDER_NO"]);
clientScopeDesc.Tables["CM_ADDRESS_BOOK"].Columns.Remove(clientScopeDesc.Tables["CM_ADDRESS_BOOK"].Columns["CATEGORY"]);
clientScopeDesc.Tables["CM_ADDRESS_BOOK"].Columns.Remove(clientScopeDesc.Tables["CM_ADDRESS_BOOK"].Columns["ABN"]);
clientScopeDesc.Tables["CM_ADDRESS_BOOK"].Columns.Remove(clientScopeDesc.Tables["CM_ADDRESS_BOOK"].Columns["PUBLIC_KEY"]);
clientScopeDesc.Tables["CM_ADDRESS_BOOK"].Columns.Remove(clientScopeDesc.Tables["CM_ADDRESS_BOOK"].Columns["USER_NAME"]);
clientScopeDesc.Tables["CM_ADDRESS_BOOK"].Columns.Remove(clientScopeDesc.Tables["CM_ADDRESS_BOOK"].Columns["URL"]);
clientScopeDesc.Tables["CM_ADDRESS_BOOK"].Columns.Remove(clientScopeDesc.Tables["CM_ADDRESS_BOOK"].Columns["COMPANY"]);
clientScopeDesc.Tables["CM_ADDRESS_BOOK"].Columns.Remove(clientScopeDesc.Tables["CM_ADDRESS_BOOK"].Columns["STD_FORM"]);
clientScopeDesc.Tables["CM_ADDRESS_BOOK"].Columns.Remove(clientScopeDesc.Tables["CM_ADDRESS_BOOK"].Columns["NO_GAP"]);
clientScopeDesc.Tables["CM_ADDRESS_BOOK"].Columns.Remove(clientScopeDesc.Tables["CM_ADDRESS_BOOK"].Columns["TX"]);
clientScopeDesc.Tables["CM_ADDRESS_BOOK"].Columns.Remove(clientScopeDesc.Tables["CM_ADDRESS_BOOK"].Columns["STAMP_CREATED_DATETIME"]);
clientScopeDesc.Tables["CM_ADDRESS_BOOK"].Columns.Remove(clientScopeDesc.Tables["CM_ADDRESS_BOOK"].Columns["STAMP_USER_ID"]);
clientScopeDesc.Tables["CM_ADDRESS_BOOK"].Columns.Remove(clientScopeDesc.Tables["CM_ADDRESS_BOOK"].Columns["STAMP_ACTION_CODE"]);
clientScopeDesc.Tables["CM_ADDRESS_BOOK"].Columns.Remove(clientScopeDesc.Tables["CM_ADDRESS_BOOK"].Columns["STAMP_DATETIME"]);
clientScopeDesc.Tables["CM_ADDRESS_BOOK"].Columns.Remove(clientScopeDesc.Tables["CM_ADDRESS_BOOK"].Columns["STAMP_DATETIME"]);
clientProvision.PopulateFromScopeDescription(clientScopeDesc);
//apply the scope definition
clientProvision.Apply();
}
// create the sync orchestrator
var syncOrchestrator = new SyncOrchestrator();
//setup providers
var localProvider = new SqlSyncProvider(scopeName, clientConn);
var remoteProvider = new SqlSyncProvider(scopeName, serverConn);
// lets intercept the changes so we can rename the columns
remoteProvider.ChangesSelected += new EventHandler<DbChangesSelectedEventArgs>(remoteProvider_ChangesSelected);
syncOrchestrator.LocalProvider = localProvider;
syncOrchestrator.RemoteProvider = remoteProvider;
// set the direction of sync session Download
syncOrchestrator.Direction = SyncDirectionOrder.Download;
// execute the synchronization process
var syncStats = syncOrchestrator.Synchronize();
// print statistics
Console.WriteLine("Start Time: " + syncStats.SyncStartTime);
Console.WriteLine("Total Changes Downloaded: " + syncStats.DownloadChangesTotal);
Console.WriteLine("Complete Time: " + syncStats.SyncEndTime);
Console.WriteLine(String.Empty);
Console.ReadKey();
}
您可以使用它们自己的结构来配置两个 table。然后,在源提供者的 ChangesSelected 事件或目标提供者的 ApplyingChanges 事件上,拦截更改数据集以进行查找并替换相应的值。
(例如,更改数据集以添加名称列,使用 ID 查找名称,将名称分配给名称列,然后在完成后删除 ID 列。)
如果同步很多行,这会减慢速度。
Sync Fx 按 table 同步,不关心 FK。数据转换也不是它的核心用例。
我正在尝试使用 Microsoft Sync Framework 来同步 2 tables。我正在使用 SyncOrchestrator 同步两个 tables.
问题是 Employees 有一个字段 cate_id,它有类别的 ID(这是类别的 FK),而 Employees2 有一个名为 Cat 的字段,它将类别的名称存储在字段直接如此(不是 fk),如果你看到一个 table 有 id 另一个名字,
如何同步那些 table?有什么建议吗?
这是我的代码
static void Main(string[] args)
{
//setup the connections
var serverConn = new SqlConnection(@"Data Source=WIN-R9D162FO6E3\HCNSQL07;User ID=mauricio;Password=Maitolin26; Initial Catalog=test;");
var clientConn = new SqlConnection(@"Data Source=WIN-R9D162FO6E3\ESP;User ID=sa;Password=Maitolin26#; Initial Catalog=MedicalDirector;");
//setup scope name
const string scopeName = "DifferentSchemaScope";
//IEnumerable<string> tablesThatChanged
//IEnumerable<string> tablesThatChanged = Enumerable.Empty<string>();
IEnumerable<string> tablesThatChanged = new string[] { "BCPeDoctors" };
//reprovision
//ReProvision(serverConn, scopeName, tablesThatChanged);
//provision server
var serverProvision = new SqlSyncScopeProvisioning(serverConn);
if (!serverProvision.ScopeExists(scopeName))
{
var serverScopeDesc = new DbSyncScopeDescription(scopeName);
// add the server table
var serverTableDesc = SqlSyncDescriptionBuilder.GetDescriptionForTable("dbo.BCPeDoctor", serverConn);
serverTableDesc.GlobalName = "BCPeDoctors";
// removing columns the source doesnt have
serverScopeDesc.Tables["BCPeDoctors"].Columns.Remove(serverScopeDesc.Tables["BCPeDoctors"].Columns["SuburbID"]);
serverScopeDesc.Tables["BCPeDoctors"].Columns.Remove(serverScopeDesc.Tables["BCPeDoctors"].Columns["Location"]);
serverScopeDesc.Tables["BCPeDoctors"].Columns.Remove(serverScopeDesc.Tables["BCPeDoctors"].Columns["SpecialtyID"]);
serverScopeDesc.Tables["BCPeDoctors"].Columns.Remove(serverScopeDesc.Tables["BCPeDoctors"].Columns["Adjusted"]);
serverScopeDesc.Tables["BCPeDoctors"].Columns.Remove(serverScopeDesc.Tables["BCPeDoctors"].Columns["Deleted"]);
serverScopeDesc.Tables["BCPeDoctors"].Columns.Remove(serverScopeDesc.Tables["BCPeDoctors"].Columns["InActive"]);
serverScopeDesc.Tables["BCPeDoctors"].Columns.Remove(serverScopeDesc.Tables["BCPeDoctors"].Columns["IsPreferred"]);
// our server and client tables has different names, so let’s setup a common name
serverScopeDesc.Tables["BCPeDoctors"].Columns["DoctorID"].IsPrimaryKey = true;
serverScopeDesc.Tables.Add(serverTableDesc);
serverProvision.PopulateFromScopeDescription(serverScopeDesc);
//apply the scope definition
serverProvision.Apply();
}
//provision client
var clientProvision = new SqlSyncScopeProvisioning(clientConn);
if (!clientProvision.ScopeExists(scopeName))
{
var clientScopeDesc = new DbSyncScopeDescription(scopeName);
// add the client table
var clientTableDesc = SqlSyncDescriptionBuilder.GetDescriptionForTable("CM_ADDRESS_BOOK", clientConn);
clientTableDesc.GlobalName = "CM_ADDRESS_BOOK2";
clientScopeDesc.Tables.Add(clientTableDesc);
clientScopeDesc.Tables["CM_ADDRESS_BOOK"].Columns["ADDRESS_BOOK_ID"].IsPrimaryKey = true;
// remove the columns the Source table doesnt have it
clientScopeDesc.Tables["CM_ADDRESS_BOOK"].Columns.Remove(clientScopeDesc.Tables["CM_ADDRESS_BOOK"].Columns["ADDRESS_BOOK_NO"]);
clientScopeDesc.Tables["CM_ADDRESS_BOOK"].Columns.Remove(clientScopeDesc.Tables["CM_ADDRESS_BOOK"].Columns["STREET_LINE_3"]);
clientScopeDesc.Tables["CM_ADDRESS_BOOK"].Columns.Remove(clientScopeDesc.Tables["CM_ADDRESS_BOOK"].Columns["CITY"]);
clientScopeDesc.Tables["CM_ADDRESS_BOOK"].Columns.Remove(clientScopeDesc.Tables["CM_ADDRESS_BOOK"].Columns["POSTCODE"]);
clientScopeDesc.Tables["CM_ADDRESS_BOOK"].Columns.Remove(clientScopeDesc.Tables["CM_ADDRESS_BOOK"].Columns["P_STREET_LINE_1"]);
clientScopeDesc.Tables["CM_ADDRESS_BOOK"].Columns.Remove(clientScopeDesc.Tables["CM_ADDRESS_BOOK"].Columns["P_STREET_LINE_2"]);
clientScopeDesc.Tables["CM_ADDRESS_BOOK"].Columns.Remove(clientScopeDesc.Tables["CM_ADDRESS_BOOK"].Columns["P_STREET_LINE_3"]);
clientScopeDesc.Tables["CM_ADDRESS_BOOK"].Columns.Remove(clientScopeDesc.Tables["CM_ADDRESS_BOOK"].Columns["P_CITY"]);
clientScopeDesc.Tables["CM_ADDRESS_BOOK"].Columns.Remove(clientScopeDesc.Tables["CM_ADDRESS_BOOK"].Columns["P_POSTCODE"]);
clientScopeDesc.Tables["CM_ADDRESS_BOOK"].Columns.Remove(clientScopeDesc.Tables["CM_ADDRESS_BOOK"].Columns["PHONE_WORK"]);
clientScopeDesc.Tables["CM_ADDRESS_BOOK"].Columns.Remove(clientScopeDesc.Tables["CM_ADDRESS_BOOK"].Columns["PAGER"]);
clientScopeDesc.Tables["CM_ADDRESS_BOOK"].Columns.Remove(clientScopeDesc.Tables["CM_ADDRESS_BOOK"].Columns["PROVIDER_NO"]);
clientScopeDesc.Tables["CM_ADDRESS_BOOK"].Columns.Remove(clientScopeDesc.Tables["CM_ADDRESS_BOOK"].Columns["CATEGORY"]);
clientScopeDesc.Tables["CM_ADDRESS_BOOK"].Columns.Remove(clientScopeDesc.Tables["CM_ADDRESS_BOOK"].Columns["ABN"]);
clientScopeDesc.Tables["CM_ADDRESS_BOOK"].Columns.Remove(clientScopeDesc.Tables["CM_ADDRESS_BOOK"].Columns["PUBLIC_KEY"]);
clientScopeDesc.Tables["CM_ADDRESS_BOOK"].Columns.Remove(clientScopeDesc.Tables["CM_ADDRESS_BOOK"].Columns["USER_NAME"]);
clientScopeDesc.Tables["CM_ADDRESS_BOOK"].Columns.Remove(clientScopeDesc.Tables["CM_ADDRESS_BOOK"].Columns["URL"]);
clientScopeDesc.Tables["CM_ADDRESS_BOOK"].Columns.Remove(clientScopeDesc.Tables["CM_ADDRESS_BOOK"].Columns["COMPANY"]);
clientScopeDesc.Tables["CM_ADDRESS_BOOK"].Columns.Remove(clientScopeDesc.Tables["CM_ADDRESS_BOOK"].Columns["STD_FORM"]);
clientScopeDesc.Tables["CM_ADDRESS_BOOK"].Columns.Remove(clientScopeDesc.Tables["CM_ADDRESS_BOOK"].Columns["NO_GAP"]);
clientScopeDesc.Tables["CM_ADDRESS_BOOK"].Columns.Remove(clientScopeDesc.Tables["CM_ADDRESS_BOOK"].Columns["TX"]);
clientScopeDesc.Tables["CM_ADDRESS_BOOK"].Columns.Remove(clientScopeDesc.Tables["CM_ADDRESS_BOOK"].Columns["STAMP_CREATED_DATETIME"]);
clientScopeDesc.Tables["CM_ADDRESS_BOOK"].Columns.Remove(clientScopeDesc.Tables["CM_ADDRESS_BOOK"].Columns["STAMP_USER_ID"]);
clientScopeDesc.Tables["CM_ADDRESS_BOOK"].Columns.Remove(clientScopeDesc.Tables["CM_ADDRESS_BOOK"].Columns["STAMP_ACTION_CODE"]);
clientScopeDesc.Tables["CM_ADDRESS_BOOK"].Columns.Remove(clientScopeDesc.Tables["CM_ADDRESS_BOOK"].Columns["STAMP_DATETIME"]);
clientScopeDesc.Tables["CM_ADDRESS_BOOK"].Columns.Remove(clientScopeDesc.Tables["CM_ADDRESS_BOOK"].Columns["STAMP_DATETIME"]);
clientProvision.PopulateFromScopeDescription(clientScopeDesc);
//apply the scope definition
clientProvision.Apply();
}
// create the sync orchestrator
var syncOrchestrator = new SyncOrchestrator();
//setup providers
var localProvider = new SqlSyncProvider(scopeName, clientConn);
var remoteProvider = new SqlSyncProvider(scopeName, serverConn);
// lets intercept the changes so we can rename the columns
remoteProvider.ChangesSelected += new EventHandler<DbChangesSelectedEventArgs>(remoteProvider_ChangesSelected);
syncOrchestrator.LocalProvider = localProvider;
syncOrchestrator.RemoteProvider = remoteProvider;
// set the direction of sync session Download
syncOrchestrator.Direction = SyncDirectionOrder.Download;
// execute the synchronization process
var syncStats = syncOrchestrator.Synchronize();
// print statistics
Console.WriteLine("Start Time: " + syncStats.SyncStartTime);
Console.WriteLine("Total Changes Downloaded: " + syncStats.DownloadChangesTotal);
Console.WriteLine("Complete Time: " + syncStats.SyncEndTime);
Console.WriteLine(String.Empty);
Console.ReadKey();
}
您可以使用它们自己的结构来配置两个 table。然后,在源提供者的 ChangesSelected 事件或目标提供者的 ApplyingChanges 事件上,拦截更改数据集以进行查找并替换相应的值。
(例如,更改数据集以添加名称列,使用 ID 查找名称,将名称分配给名称列,然后在完成后删除 ID 列。)
如果同步很多行,这会减慢速度。
Sync Fx 按 table 同步,不关心 FK。数据转换也不是它的核心用例。