索引Indexes

注意

本節中的設定是一般適用於關聯式資料庫。The configuration in this section is applicable to relational databases in general. 當您安裝的關聯式資料庫提供者,如下所示的擴充方法會變成可用 (因為共用Microsoft.EntityFrameworkCore.Relational封裝)。The extension methods shown here will become available when you install a relational database provider (due to the shared Microsoft.EntityFrameworkCore.Relational package).

關聯式資料庫中的索引對應到相同的概念與 Entity Framework 的核心中的索引。An index in a relational database maps to the same concept as an index in the core of Entity Framework.

慣例Conventions

根據慣例,索引會命名為IX_<type name>_<property name>By convention, indexes are named IX_<type name>_<property name>. 針對複合索引<property name>變成屬性名稱的底線分隔清單。For composite indexes <property name> becomes an underscore separated list of property names.

資料註釋Data Annotations

索引不可以使用資料註解來設定。Indexes can not be configured using Data Annotations.

關於 fluent 應用程式開發介面Fluent API

您可以使用 fluent 應用程式開發的應用程式開發介面來設定索引的名稱。You can use the Fluent API to configure the name of an index.

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

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Blog>()
            .HasIndex(b => b.Url)
            .HasName("Index_Url");
    }
}

public class Blog
{
    public int BlogId { get; set; }
    public string Url { get; set; }
}