none
How to change sync table schema of Local Data Cache? RRS feed

  • Question

  • Hi

    I have searched the internet a lot but have not found an answer to my question.

    I have a table on my server DB that uses a uniqueidentifier as the PK with a default value 'newID()'. This default value is not copied when the local Data Cache creates my SDF on the client. I tried making a change to the schema in the code behind of the LocalDataCacheClientSyncProvider on the CreatingSchema event, but this event NEVER seems to fire. I would like the schema on the client also to have 'newID()' as the default value on the PK, as the sync direction for this table is bi-directional and I don't want any PK conflicts.

    The code I've used in 'CreateSchema' is the following:

    If e.Table.TableName = "Users" Then

          If (e.Schema.Tables("Users").Columns("UserID").DataType Is GetType(System.Guid)) Then

                e.Schema.Tables("Users").Columns("UserID").DefaultValue = "NEWID()"

          End If

    End If

    So my question is: how can I change the schema of the Local Data Cache? And if that is not possible, how else would I go about to get the default value on my client to be Newid()

    Any help is greatly appreciated.

    Sunday, July 3, 2011 2:52 PM

Answers

  • 'DropExistingOrCreateNewTable' should re-create the schema. which part of the generated code did you setup the event handler for the "CreatingSchema" event?
    • Marked as answer by CWinks Sunday, July 3, 2011 4:31 PM
    Sunday, July 3, 2011 3:53 PM
    Moderator
  • 1 - you probably didnt have to setup event handlers if you're referring to UI controls where you simply double click on it and write code for the specific event you want to handle. Sync Framework has no UI, so you have to explicitly specify which event you want to subscribe to.

    2. - are you passing a "null" value to the database? afaik, Defaults kick in if you don't pass a value for that column. If you're passing "null", then the database thinks you really want to save it as "null".

    • Marked as answer by CWinks Monday, July 4, 2011 5:42 AM
    Monday, July 4, 2011 1:05 AM
    Moderator

All replies

  • i think this is the same question as posted here: http://stackoverflow.com/questions/6562123/local-data-cache-synctable-schema-not-copied-clientsyncprovider-creatingschema

    Are you re-creating the SDF created by the Local Database Cache Wizard? If not, have a look at the TableCreation option.

    please see: http://msdn.microsoft.com/en-us/library/bb726037.aspx

    Sunday, July 3, 2011 3:12 PM
    Moderator
  • Thanks for the reply.

    The TableCreation option on my LocalDataCache is 'DropExistingOrCreateNewTable'. Is this the correct option for what I'd like to do?

    I'm new to this whole topic, so any explanation would really be helpful.

    • Marked as answer by CWinks Sunday, July 3, 2011 4:31 PM
    • Unmarked as answer by CWinks Sunday, July 3, 2011 4:31 PM
    Sunday, July 3, 2011 3:28 PM
  • 'DropExistingOrCreateNewTable' should re-create the schema. which part of the generated code did you setup the event handler for the "CreatingSchema" event?
    • Marked as answer by CWinks Sunday, July 3, 2011 4:31 PM
    Sunday, July 3, 2011 3:53 PM
    Moderator
  • As I read your reply I realized that indeed, I had not set up an event handler for the CreatingSchema event. I thought that it would just fire without setting it up. I did the following  - is this correct the correct place to put it?

    Partial Class WTSLocalDataCacheClientSyncProvider

        Public Overrides Sub onInitialized()
            AddHandler Me.CreatingSchema, AddressOf WTSLocalDataCacheClientSyncProvider_CreatingSchema
        End Sub

    ...

    end class

    And when setting a breakpoint in the sub, it indeed fired! So thank you for helping me out with this!

    Now I have another two questions

    1) Why did I have to manually set up this event handler? I have never had to do this in VB.NET, although I've seen it done in many times in C#.

    2) On my form where I edit the client DB, when I add a new row to the datagrid and validate it, it throws the following exception: No Null Allowed Exception: Column "UserID" does not allow Nulls. It is clear from this message that the GUID is not created automatically upon entering a new row. How come? Didn't I set up a default value with "newID()"?

    Thank you!

     

    Sunday, July 3, 2011 4:31 PM
  • 1 - you probably didnt have to setup event handlers if you're referring to UI controls where you simply double click on it and write code for the specific event you want to handle. Sync Framework has no UI, so you have to explicitly specify which event you want to subscribe to.

    2. - are you passing a "null" value to the database? afaik, Defaults kick in if you don't pass a value for that column. If you're passing "null", then the database thinks you really want to save it as "null".

    • Marked as answer by CWinks Monday, July 4, 2011 5:42 AM
    Monday, July 4, 2011 1:05 AM
    Moderator
  • 1-Thank you! That explains things. I wonder why I didn't have to set up any event handler for the serversyncprovider's applyChangeFailed event.

    2-I'll look into it. Thanks for the tip.

    Monday, July 4, 2011 5:42 AM
  • I entered the following line in the Form_Load event of my form that has the datagridview:

            Me.MainDataSet.Users.Columns(MainDataSet.Users1.UserIDColumn.ColumnName).DefaultValue = Guid.NewGuid

    This solved the Null Insert problem, as it creates a new GUID for every new column added.

     

    Monday, July 4, 2011 11:40 AM