This project has moved and is read-only. For the latest updates, please go here.

Key Generation Strategy

Jan 21, 2011 at 8:37 AM


i had issues with saving to sterling as well until i figured out that i have to give each new entry a new ID.

this is something i didnt expect after working with sql-server a long time ;)

so whats the recommended strategy to generate keys?

- select max row and insert new one with +1?

- generate a PrimaryKeyProvider<T> class that uses IsolatedStorage for saving the last id?

- use GUID as a key?


pls give advice and maybe integrate it in the product? :)



Jan 21, 2011 at 9:57 AM

GUID is always easiest. Remember, this isn't a relational SQL database, it's an object database that gives you the flexibility to have any serializable value as a key, not just identities. That's why Sterling doesn't make assumptions nor try to understand.

If you want to go with traditional id-like keys, or any other strategy, the trigger:

Is the ideal way to go. The post works equally well for Windows Phone or traditional Silverlight. You can also look at the unit tests for triggers, those are coded to auto-generate ids as well.

Jan 21, 2011 at 12:13 PM
Edited Jan 21, 2011 at 12:15 PM

hi jeremy,

thx for the fast answer. my problem was more, that saving didnt work because i didnt assign an ID in the first place. actually i dont really need an ID since i work in a stateful environment (wp7) anyways and its enough to have linq capabilities to load my data without refering to identity columns.

i will go ahead and have a look on the unint tests to see how you handle auto-generated ids there.