开发者

How to make a field value unique in Entity Framework

开发者 https://www.devze.com 2023-02-20 23:35 出处:网络
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.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消