Skip to content

Commit

Permalink
Document index sort ordering
Browse files Browse the repository at this point in the history
Closes #3691
  • Loading branch information
roji committed Jul 21, 2022
1 parent 2ce95cf commit 4c85437
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 1 deletion.
31 changes: 31 additions & 0 deletions entity-framework/core/modeling/indexes.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,37 @@ By default, indexes aren't unique: multiple rows are allowed to have the same va

Attempting to insert more than one entity with the same values for the index's column set will cause an exception to be thrown.

## Index sort order

> [!NOTE]
> This feature is being introduced in EF Core 7.0.
In most databases, each column covered by an index can be either ascending or descending. For indexes covering only one column, this typically does not matter: the database can traverse the index in reverse order asif needed. However, for composite indexes, the ordering can be crucial for good performance, and can mean the difference between an index getting used by a query or not. In general, the index columns' sort orders should correspond to those specified in the `ORDER BY` clause of your query.

The index sort order is ascending by default. You can make all columns have descending order as follows:To make all columns be descending,

### [Data Annotations](#tab/data-annotations)

[!code-csharp[Main](../../../samples/core/Modeling/IndexesAndConstraints/DataAnnotations/IndexDescending.cs?name=IndexDescending&highlight=1)]

### [Fluent API](#tab/fluent-api)

[!code-csharp[Main](../../../samples/core/Modeling/IndexesAndConstraints/FluentAPI/IndexDescending.cs?name=IndexDescending&highlight=5)]

***

You may also specify the sort order on a column-by-column basis as follows:

### [Data Annotations](#tab/data-annotations)

[!code-csharp[Main](../../../samples/core/Modeling/IndexesAndConstraints/DataAnnotations/IndexDescendingAscending.cs?name=IndexDescendingAscending&highlight=1)]

### [Fluent API](#tab/fluent-api)

[!code-csharp[Main](../../../samples/core/Modeling/IndexesAndConstraints/FluentAPI/IndexDescendingAscending.cs?name=IndexDescendingAscending&highlight=5)]

***

## Index name

By convention, indexes created in a relational database are named `IX_<type name>_<property name>`. For composite indexes, `<property name>` becomes an underscore separated list of property names.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using Microsoft.EntityFrameworkCore;

namespace EFModeling.IndexesAndConstraints.DataAnnotations.IndexDescending;

internal class MyContext : DbContext
{
public DbSet<Blog> Blogs { get; set; }
}

#region IndexDescending
[Index(nameof(Url), nameof(Rating), IsDescending = new bool[0])]
public class Blog
{
public int BlogId { get; set; }
public string Url { get; set; }
public int Rating { get; set; }
}
#endregion
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using Microsoft.EntityFrameworkCore;

namespace EFModeling.IndexesAndConstraints.DataAnnotations.IndexDescendingAscending;

internal class MyContext : DbContext
{
public DbSet<Blog> Blogs { get; set; }
}

#region IndexDescendingAscending
[Index(nameof(Url), nameof(Rating), IsDescending = new[] { false, true })]
public class Blog
{
public int BlogId { get; set; }
public string Url { get; set; }
public int Rating { get; set; }
}
#endregion
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using Microsoft.EntityFrameworkCore;

namespace EFModeling.IndexesAndConstraints.FluentAPI.IndexDescending;

internal class MyContext : DbContext
{
public DbSet<Blog> Blogs { get; set; }

#region IndexDescending
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Blog>()
.HasIndex(b => new { b.Url, b.Rating })
.IsDescending();
}
#endregion
}

public class Blog
{
public int BlogId { get; set; }
public string Url { get; set; }
public int Rating { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using Microsoft.EntityFrameworkCore;

namespace EFModeling.IndexesAndConstraints.FluentAPI.IndexDescendingAscending;

internal class MyContext : DbContext
{
public DbSet<Blog> Blogs { get; set; }

#region IndexDescendingAscending
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Blog>()
.HasIndex(b => new { b.Url, b.Rating })
.IsDescending(false, true);
}
#endregion
}

public class Blog
{
public int BlogId { get; set; }
public string Url { get; set; }
public int Rating { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.0-preview.6.22329.4" />
</ItemGroup>

</Project>

0 comments on commit 4c85437

Please sign in to comment.