7

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?

5
  • 1
    Do you have the relevant using statement in the file with this code in?
    – DavidG
    Commented Sep 25, 2014 at 16:29
  • Yes - I have using System.ComponentModel.DataAnnotations; , and the [Key] attribute is accepted.
    – Mike
    Commented Sep 25, 2014 at 16:31
  • Which version of .NET Framework your projects are using?
    – Masoud
    Commented Sep 25, 2014 at 16:31
  • .NET 4.0 full profile
    – Mike
    Commented Sep 25, 2014 at 16:33
  • I still need to support XP and Server 2003 clients.
    – Mike
    Commented Sep 25, 2014 at 16:36

2 Answers 2

10

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;
3
  • 3
    I have the same problem. Adding Schema with a using statement didn't help. Commented Jan 11, 2015 at 23:04
  • 1
    Worked perfectly for me. But I had already upgraded my package to 6.1.2. Note that the Visual Studio 2013 Community Edition uses 6.0.something out of the <virtual> box and you have to upgrade to the new version of EF before the Using will help.
    – Anne Gunn
    Commented Jan 16, 2015 at 0:54
  • @MeetingAttender you may be on an older version of .Net. Try using Microsoft.EntityFrameworkCore;
    – Kake_Fisk
    Commented Feb 15, 2022 at 9:19
4

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.