EF Core - 复合主键删除一个索引

问题描述 投票:0回答:2

我有这个实体:

[Table("order")]
public class Order
{
    [Key]
    [Column("id")]
    public Guid Id { get; set; }

    [MaxLength(128)]
    [Column("description")]
    public string Description { get; set; }

    [ForeignKey("Student")]
    [Column("student_id")]
    public Guid StudentId { get; set; }

    [ForeignKey("Employer")]
    [Column("employer_id")]
    public Guid EmployerId { get; set; }

    [ForeignKey("Customer")]
    [Column("customer_id")]
    public Guid CustomerId { get; set; }

    public virtual Guid Student { get; set; }
    public virtual Guid Employer { get; set; }
    public virtual Guid Customer { get; set; }
}

我看到DbContext的下一个快照:

modelBuilder.Entity("DataInterface.Models.Order", b =>
{
    b.Property<Guid>("Id")
        .ValueGeneratedOnAdd()
        .HasColumnName("id");

    b.Property<string>("Description")
        .HasColumnName("description")
        .HasMaxLength(128);

    b.Property<Guid>("StudentId")
        .HasColumnName("student_id");

    b.Property<Guid>("EmployerId")
        .HasColumnName("employer_id");

    b.Property<Guid>("CustomerId")
        .HasColumnName("customer_id");

    b.HasKey("Id");

    b.HasIndex("StudentId");

    b.HasIndex("EmployerId");

    b.HasIndex("CustomerId");

    b.ToTable("order");
});

你怎么能看到 - 一切都很好。但我需要创建一个复合键并删除主键。那么,我们来做吧。我的实体新代码(没有Id主键):

[Table("order")]
public class Order
{
    [MaxLength(128)]
    [Column("description")]
    public string Description { get; set; }

    [ForeignKey("Student")]
    [Column("student_id")]
    public Guid StudentId { get; set; }

    [ForeignKey("Employer")]
    [Column("employer_id")]
    public Guid EmployerId { get; set; }

    [ForeignKey("Customer")]
    [Column("customer_id")]
    public Guid CustomerId { get; set; }

    public virtual Guid Student { get; set; }
    public virtual Guid Employer { get; set; }
    public virtual Guid Customer { get; set; }
}

我将新行为添加到我的上下文的OnModelCreating方法中:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Entity<Order>()
        .HasKey(x => new { x.StudentId, x.EmployerId, x.CustomerId });
}

当我添加新的迁移时,我看到StudentId的索引被删除了。我不明白为什么?我对DbContext的新快照:

modelBuilder.Entity("DataInterface.Models.Order", b =>
{
    b.Property<Guid>("Id")
        .ValueGeneratedOnAdd()
        .HasColumnName("id");

    b.Property<string>("Description")
        .HasColumnName("description")
        .HasMaxLength(128);

    b.Property<Guid>("StudentId")
        .HasColumnName("student_id");

    b.Property<Guid>("EmployerId")
        .HasColumnName("employer_id");

    b.Property<Guid>("CustomerId")
        .HasColumnName("customer_id");

    b.HasKey("StudentId", "EmployerId", "CustomerId");

    b.HasIndex("EmployerId");

    b.HasIndex("CustomerId");

    b.ToTable("order");
});

为什么删除了三个索引中的一个?为什么b.HasIndex("StudentId");线被删除了?怎么了?

注意:我没有看到来自另一个表中的两个字段的复合键的这个问题。

c# entity-framework entity-framework-core ef-migrations
2个回答
3
投票

主键由索引强制执行。

StudentId是该指数的第一列。

它不需要在其上定义另一个索引(这样的索引几乎肯定也毫无意义,因为PK索引可能是表的聚簇索引,非聚簇索引包含其叶子上的PK列)


1
投票

关于StudentId的另一个索引将是多余的。由于这是主键中的第一列,因此对StudentId的查找可以简单地使用其索引。类似地,对StudentId + EmployerId的查找也可以这样做。

有趣的是,这曾经不是EF Core的情况,并且创建了两个索引。现在删除索引的事实意味着已修复/优化此行为。

© www.soinside.com 2019 - 2024. All rights reserved.