views:

173

answers:

2

I get this error when using linq-to-sql with timestamp as part of a composite primary key:

"The primary key column of type 'Timestamp' cannot be generated by the server."

I'm guessing this may be due to the fact timestamp is just a row version thus perhaps it must be created after the insert? Or...

+5  A: 

don't use the timestamp data type!!

The timestamp syntax is deprecated. This feature will be removed in a future version of Microsoft SQL Server. Avoid using this feature in new development work, and plan to modify applications that currently use this feature.

timestamp (Transact-SQL) http://msdn.microsoft.com/en-us/library/ms182776(SQL.90).aspx rowversion (Transact-SQL) http://msdn.microsoft.com/en-us/library/ms182776.aspx

Also, if it primarily designed to change, to keep track of versions, why make it a part of a primary key? changing a primary key can cause many problems!

If you need a system generated value for a primary key, use an identity or guid.

IDENTITY (Property) http://msdn.microsoft.com/en-us/library/aa933196(SQL.80).aspx
GUID uniqueidentifier http://msdn.microsoft.com/en-us/library/aa260656(v=SQL.80).aspx

KM
@KM Thanks for the info but doesn't linq-to-sql require timestamp for optimistic concurrency for detached objects? The main reason I'm doing this is that I really don't need a primary key for this table or rather I don't care if there are duplicate entries provided that not every field is duplicated. Basically, I'm using this as a token storage for a password recovery and the user may request password multiple times. I just store a new token in the table versus because I don't want to check if it already exists.
Curtis White
@KM Also rowversion is not an option in Visual Studio 2008.
Curtis White
try making the PK an identity and the timestamp just a column, like in this example: http://mtaulty.com/CommunityServer/blogs/mike_taultys_blog/archive/2008/07/01/10557.aspx see this link about rowversion http://thedatafarm.com/blog/data-access/if-timestamp-is-deprecated-why-do-none-of-the-current-db-tools-support-rowversion/
KM
@Curtis: LINQ-to-SQL does use the timestamp/rowversion column for optimistic concurrency checks, but the LINQ engine automatically scans the table schema to see if one is present. Since MSSQL only allows one timestamp/rowversion field per table, it's an easy check. Regarding VS2008, you can't enter rowversion as a data type, but timestamp is converted to rowversion behind the scenes.
Neil T.
A: 

You can work around it.. set

  • Auto Generated Value to True
  • Auto-Sync to OnInsert

...unless you already have of course

gbn
@gbn Thanks. Sounds like that could work but I'll probably just not use it in the PK because I don't want to modify the DBML.
Curtis White