Index Attribute
Improve EF Core performance with EF Extensions
Index Attribute
Entity Framework 6 provides the Index attribute to create an index on a particular column in the database.
public class Book
{
public int Id { get; set; }
[Index]
public string Title { get; set; }
public string Category { get; set; }
public int AuthorId { get; set; }
public Author Author { get; set; }
}But in EF Core, indexes cannot be created using data annotations. You have to use the Fluent API to specify an index on a column as shown below.
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Book>()
.HasIndex(b => b.Title);
}Now in EF Core, the new Index attribute can be placed on an entity type to specify an index for one or more columns.
Now when you add a migration, you will see that the index is created for the Title column.
You can also use the Index attribute to specify an index spanning multiple columns.
For SQL Server, Migrations will then generate the following SQL.
Last updated
