Due to the sync framework insert trigger my business logic was broke. I was getting resulted in a StaleStateException (-1 rows affected, expected 1). error. I looked at the sync insert trigger and found a strange thing.
There is an update call before inserting to the tracking table. This update call joins with the tracking table for inserted record. How it is going update the record which really do not exists! I removed the UPDATE call and it works fine now.
However I am not sure the reason behind the presence of the UPDATE call withing the INSERT trigger.
Here is the sample trigger:
ALTER TRIGGER MYTABLE_insert_trigger] ON MYTABLE FOR INSERT AS
UPDATE [side] SET [sync_row_is_tombstone] = 0, [local_update_peer_key] = 0, [restore_timestamp] = NULL, [update_scope_local_id] = NULL, [last_change_datetime] = GETDATE(), [ID] = [i].[ID] FROM [sync].[MYTABLE_tracking] [side] JOIN INSERTED [i] ON [side].[PrimaryKey1]
= [i].[PrimaryKey1] AND [side].[PrimaryKey2] = [i].[PrimaryKey2]
INSERT INTO [sync].[MYTABLE_tracking] ([i].[PrimaryKey1], [i].[PrimaryKey2], [create_scope_local_id], [local_create_peer_key], [local_create_peer_timestamp], [update_scope_local_id], [local_update_peer_key], [sync_row_is_tombstone], [last_change_datetime],
[restore_timestamp], [i].[ID]) SELECT [i].[PrimaryKey1], [i].[PrimaryKey2], NULL, 0, @@DBTS+1, NULL, 0, 0, GETDATE(), NULL, [i].[ID] FR
View Complete Post