微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

EF Core迁移从新的复合索引中删除了第一个外键索引

如何解决EF Core迁移从新的复合索引中删除了第一个外键索引

这是我的模特:

public class Period
{
    [Key]
    public int Id { get; set; }
}

public class Company
{
    [Key]
    public int Id { get; set; }
}

public class Invoice
{
    [Key]
    public int Id { get; set; }

    public int PeriodId { get; set; }
    public Period Period { get; set; }

    public int CompanyId { get; set; }
    public Company Company { get; set; }
}

没有幻想。具有期间和公司的简单发票。

以下是它们的配置方式:

modelBuilder.Entity<Invoice>()
    .HasOne(p => p.Period)
    .WithMany()
    .OnDelete(DeleteBehavior.Restrict)
;

modelBuilder.Entity<Invoice>()
    .HasOne(p => p.Company)
    .WithMany()
    .OnDelete(DeleteBehavior.Restrict)
;

这是从中生成的迁移文件

migrationBuilder.CreateTable(
    name: "Companies",columns: table => new
    {
        Id = table.Column<int>(nullable: false)
            .Annotation("sqlServer:Identity","1,1")
    },constraints: table =>
    {
        table.PrimaryKey("PK_Companies",x => x.Id);
    });

migrationBuilder.CreateTable(
    name: "Periods",constraints: table =>
    {
        table.PrimaryKey("PK_Periods",x => x.Id);
    });

migrationBuilder.CreateTable(
    name: "Invoices",1"),PeriodId = table.Column<int>(nullable: false),CompanyId = table.Column<int>(nullable: false)
    },constraints: table =>
    {
        table.PrimaryKey("PK_Invoices",x => x.Id);
        table.ForeignKey(
            name: "FK_Invoices_Companies_CompanyId",column: x => x.CompanyId,principalTable: "Companies",principalColumn: "Id",onDelete: referentialAction.Restrict);
        table.ForeignKey(
            name: "FK_Invoices_Periods_PeriodId",column: x => x.PeriodId,principalTable: "Periods",onDelete: referentialAction.Restrict);
    });

migrationBuilder.CreateIndex(
    name: "IX_Invoices_CompanyId",table: "Invoices",column: "CompanyId");

migrationBuilder.CreateIndex(
    name: "IX_Invoices_PeriodId",column: "PeriodId");

一切似乎都是合法的。现在,我需要创建一个唯一索引,该索引可以控制一段时间内公司发票的唯一性:

modelBuilder.Entity<Invoice>()
    .HasIndex(p => new
    {
        p.PeriodId,p.CompanyId,})
    .IsUnique()
;

这就是现在的迁移情况:

migrationBuilder.DropIndex(
    name: "IX_Invoices_PeriodId",table: "Invoices");

migrationBuilder.CreateIndex(
    name: "IX_Invoices_PeriodId_CompanyId",columns: new[] { "PeriodId","CompanyId" },unique: true);

注意“ DropIndex”。它从我的综合索引中获取第一列。如果我更改顺序,它仍然会删除一个

modelBuilder.Entity<Invoice>()
    .HasIndex(p => new
    {
        p.CompanyId,p.PeriodId,})
    .IsUnique()
;

migrationBuilder.DropIndex(
    name: "IX_Invoices_CompanyId",table: "Invoices");

migrationBuilder.CreateIndex(
    name: "IX_Invoices_CompanyId_PeriodId",columns: new[] { "CompanyId","PeriodId" },unique: true);

我不确定我是否理解这背后的逻辑。复合索引中是否有什么特别之处使单独的索引无用?或者也许是某种错误

我发现的关于此问题的唯一提及是关于SO的一些过时的帖子: EF Migrations drops index when adding compsite index

P.S。使用sql Server作为数据库提供程序在3.1.8上进行了测试。

解决方法

应该更彻底地研究这个问题。 创建复合索引时,它的第一列可以在查询中单独使用。 https://github.com/dotnet/efcore/issues/22513

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。