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

entity-framework entity-framework-6 indexing

Question

In my model, I'm attempting to transform a column into a unique key.

My code is

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

I also cite the following sources:

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

I'm still getting

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

Do I have anything missing? I'm use EF 6.1.

I appreciate you.

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

Popular Answer

Rephrase your response. (If abbreviated URLs are now permitted, we apologize)

The Index attribute is only present in the file in.net 4.5. (EntityFramework.dll)

The Index attribute is not present in the Dll System.ComponentModel.DataAnnotations.Schema.

This characteristic is present in the Version 6 of EntityFramework.dll or later.

When you add a reference to the file EntityFramework.dll, the error will go away and the code will compile.

The system of namespaces. ComponentModel. DataAnnotations. Schema is included in this EntityFramework.dll as well.

It's probably a Microsoft error that will be fixed 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