问题
In SQL Server CE can I create an identity column with data type uniqueidentifier
? If so, how? If not, why not?
I have tried creating it with "New Table"from the Server Explorer in VS2010, and the Identity option only gets enabled when the type is int.
Edit:
According to EricEJ's suggestion I did make the column PK. I also set it to be a "RowGuid"
CREATE TABLE [MyTable] (
[Id] uniqueidentifier ROWGUIDCOL
...
I don't know exactly the intended purpose of the RowGuid
property, but it seem to have the desired effect, namely to automatically create new default guid-values upon insert, without having to create them in code. It would be nice to have the default values to be sequential guids, but this doesn't seem to be supported by SQL Server CE.
回答1:
The only supported type for IDENTITY is int or bigint. But you can have a Guid/uniqueidentifier as primary key, and set a value of this in code using Guid.NewGuid().
来源:https://stackoverflow.com/questions/8763609/uniqueidentifier-as-identity-in-sql-server-ce