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? Commented Sep 25, 2014 at 16:29
  • Yes - I have using System.ComponentModel.DataAnnotations; , and the [Key] attribute is accepted. Commented Sep 25, 2014 at 16:31
  • Which version of .NET Framework your projects are using? Commented Sep 25, 2014 at 16:31
  • .NET 4.0 full profile Commented Sep 25, 2014 at 16:33
  • I still need to support XP and Server 2003 clients. 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;
Sign up to request clarification or add additional context in comments.

3 Comments

I have the same problem. Adding Schema with a using statement didn't help.
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.
@MeetingAttender you may be on an older version of .Net. Try using Microsoft.EntityFrameworkCore;
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

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.