How to add Unique Key for a column in a model in EF code first

entity-framework entity-framework-6 indexing

Question

I am trying to make a column as Unique key in my model.

my code is :

public class Customer
{
    [Index("abcd",IsUnique = true)]
    public int CustomerID { get; set; }
}

and I am using following references:

using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

Still i am getting

Error   103 The type or namespace name 'IndexAttribute' could not be found (are you missing a using directive or an assembly reference?)    

Am i missing anything? Iam using EF 6.1 version

Thanks in advance

1
3
11/25/2014 9:51:25 AM

Popular Answer

Rephrase my answer. (apologies if shortened URL's are now allowed)

In .net 4.5 the Index attribute only exists in the file (EntityFramework.dll)

The Dll System.ComponentModel.DataAnnotations.Schema does not contain the Index attribute

The EntityFramework.dll version 6 or later contains this attribute

Browse to the file EntityFramework.dll and add a reference the error will vanish and the code will compile.

The namespace System.ComponentModel.DataAnnotations.Schema is also part of this EntityFramework.dll

I assume its a blunder by Microsoft and may be corrected in .net 5

4
2/17/2015 2:42:41 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow