1

I have a table with a key and another field named "CardId" . Now I want to make CardId unique. I mean just unique value should be store in CardId. How can I do it? thanks

Tony
  • 1,566
  • 2
  • 15
  • 27
Mohammad
  • 11
  • 2

1 Answers1

3

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.

tobias86
  • 4,979
  • 1
  • 21
  • 30
  • 1
    +1 correct answer. Current EF version doesn't support unique keys at all but it is hopefully planned for the next major version: http://www.ladislavmrnka.com/2011/03/unique-key-constraints-in-entity-framework/ – Ladislav Mrnka Mar 29 '11 at 09:37