1

Hopefully someone can help me. I have a Visual Studio 2008 WinForms application to which I have added a Local Data Cache. The local data cache is using a SDF file via SqlServerCE and is caching a SQL Server 2008 dbase with change tracking enabled.

This is working and I can do bidirectional syncs and the like.

The problem I am having is with inserting data in to the local tables that have been created by the VS wizards. When I try insert data either programmatically or even through the VS interface (show data) I get a error stating “A duplicate value cannot be inserted into a unique index” due to a primary key constraint.

The tables have a auto inc int primary key and it seems like the seed has been reset back to one. This doesn’t happen to all the tables though, only a few.

Any ideas how I can fix this?

Regards, Adam

Rui Jarimba
  • 11,166
  • 11
  • 56
  • 86

3 Answers3

0
  • can you post the Server DDL Statement for the relevant table?
Community
  • 1
  • 1
Martin Dürrmeier
  • 1,653
  • 5
  • 18
  • 35
0

I had same issue, and replaced Int/auto-incremental values with GUIDs. works good for me.

0

I had the same problem. I fixed it by making each primary key on each of my tables a GUID. This way, when records are created on the client in an offline environment, and then synced back with the server, there are no conflicts between the client/server primary keys. This also prevents conflicts with the IDENTITY attributed being auto incremented on each INSERT to a value that already exists.

As seen in the below screenshot, the Data Type is set to nvarchar(50) (it can be any string-like Data Type), the Default Value is set to (newid()), and the Identity Specification is set to No.

GUID attribute screenshot in SQL Server Management Studio: GUID attribute screenshot in SQL Server Management Studio

You can read more about the GUID Data Type from Microsoft's documentation, and see if they're appropriate for your particular scenario.

Mogsdad
  • 44,709
  • 21
  • 151
  • 275
zackbetz
  • 1
  • 2