如何在代码优先的Entity Framework 6.1.1中使用[Index]属性

本文关键字:属性 Index Framework 代码 Entity | 更新日期: 2023-09-27 18:24:21

我首先使用实体框架6.1.1和代码。EF 6.1应该增加对Index属性的支持,但编辑器自动完成或编译器都不接受[Index]属性,例如:

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

所有项目参考都指向EF 6.1.1的DLL。

我还运行SQL Server Compact Edition 4.0。

如何启用新的[Index]属性?

如何在代码优先的Entity Framework 6.1.1中使用[Index]属性

KeyAttributeSystem.ComponentModel.DataAnnotations命名空间中时,IndexAttribute类在System.ComponentModel.DataAnnotations.Schema命名空间中。您可能缺少以下using语句:

using System.ComponentModel.DataAnnotations.Schema;

请注意,如果您在编写时使用ASP.NET Core,则不支持Index属性。在这篇SO文章中很好地概述了另一种选择:Asp.net核心实体框架找不到IndexAttribute