3

I'm using EF6.1 with Model First approach, and I want to add an index to some database columns.

Is there a more convenient way than the one described in Add index in EF Model First design?

I would prefer to generate the index in the initial SQL, and avoid a migration.

Community
  • 1
  • 1
  • After searching over and over again, I also found no way to do it other than ["Influencing the DDL Generation"](https://msdn.microsoft.com/en-us/data/ff830362.aspx) suggests. Sad thing that this hasn't changed in 5 years. – Uwe Keim Nov 12 '15 at 06:39

1 Answers1

3

EF 6.1 added IndexAttribute so that you can specify an index on a property like so:

public class Post 
{ 
    public int Id { get; set; } 
    public string Title { get; set; } 
    public string Content { get; set; } 
    [Index] 
    public int Rating { get; set; } 
    public int BlogId { get; set; } 
}

It is talked about in detail here : http://msdn.microsoft.com/en-us/data/jj591583.aspx#Index

Britton
  • 2,965
  • 2
  • 22
  • 24
  • 1
    This is for "Code First Data Annotations", as title suggests. I'm using Model First approach. When I try to edit my partial class, it naturally doesn't allow me to just override an existing property, and even if it did, it would not have an effect on TSQL generation. – analytik_work Aug 07 '14 at 13:40