开发者

Microsoft Sync - Not bringing over Foreign Keys

开发者 https://www.devze.com 2023-02-22 12:56 出处:网络
I am using Microsoft Sync Frameowrk, and it is bringing over my tables, but it is not pulling over the Foreign Keys? I have made sure that the parent (PK Table) is bring brought over first as I would

I am using Microsoft Sync Frameowrk, and it is bringing over my tables, but it is not pulling over the Foreign Keys? I have made sure that the parent (PK Table) is bring brought over first as I would assume that would cause the FKs to fail.

What am I missing here that is not allowing the FKs to be brought with the sync?

开发者_高级运维

Using SQL Server 2008 R2 and SQL Server 2008 R2 Express. I believe I am using the Sync 2.1 Framework.

Any suggestions would be greatly appreciated.

Thanks,

S


Sync Fx doesnt provision FKs and other constraints other than the PK. you will have to script out your FKs and apply them programmatically.


The ServerSyncProvider creates a DataSet that the ClientSyncProvider uses to populate tables on the client database. The functionality provided by default (as you mention) does not include foreign key constraints. (I dont know why... it seems that would make sense)

It's probably easiest to override the ServerSyncProvider's GetSchema method like this:

public override SyncSchema GetSchema(Collection<string> tableNames,SyncSession syncSession)
{
  var schema = base.GetSchema(tableNames,syncSession);
  schema.SchemaDataSet.Tables["Table1"].Constraints.Add(new ForeignKeyConstraint(...));
  return schema;
}

The ClientSyncProvider will script in the foreign keys based on what it find in the dataset

Of course, you probably want to pull out the foreign keys from your database programmatically like in http://blog.sqlauthority.com/2006/11/01/sql-server-query-to-display-foreign-key-relationships-and-name-of-the-constraint-for-each-table-in-database/

0

精彩评论

暂无评论...
验证码 换一张
取 消