How to make a field value unique in Entity Framework
I have a table with a key and another field named "Card开发者_运维问答Id" . Now I want to make CardId unique. I mean just unique value should be store in CardId. How can I do it? thanks
Assuming you use SQL Server, you can simply add an unique index to your table, e.g.
CREATE UNIQUE NONCLUSTERED INDEX [IX_TableName] ON [dbo].[TableName]
(
[CardId] ASC
) ON [PRIMARY]
GO
This will ensure that the column only allows unique values to be stored.
As far as I'm aware, the only way to specify a column as "unique" in EF4 is to flag it as the Entity Key (please correct me if I'm wrong), but this could lead to some confusion down the line as your Entity Key should ideally map to the primary key of your table.
精彩评论