简体   繁体   中英

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?

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;

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM