Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use [Index] attribute in Entity Framework 6.1.1 with Code First

I'm using Entity Framework 6.1.1 and Code first. EF 6.1 should have added support for the Index attribute, but neither the editor auto-completion or compiler accept the [Index] attribute such as:

[Index]
public DateTime TimeOfSale { get; set; }

All project references point to the DLLs for EF 6.1.1.

I'm also running SQL Server Compact Edition 4.0.

How to enable the use of the new [Index] attribute?

like image 269
Mike Avatar asked Sep 25 '14 16:09

Mike


2 Answers

While the KeyAttribute is in the System.ComponentModel.DataAnnotations namespace, the IndexAttribute class is in the System.ComponentModel.DataAnnotations.Schema namespace. You are likely missing the following using statement:

using System.ComponentModel.DataAnnotations.Schema;
like image 75
DavidG Avatar answered Oct 16 '22 16:10

DavidG


Note if you are working in ASP.NET Core as of this writing the Index attribute is not supported. An alternative is nicely outlined in this SO post though: Asp.net Core Entity Framework cannot find IndexAttribute

like image 34
Opsimath Avatar answered Oct 16 '22 16:10

Opsimath