为什么EF下降并创建相同的索引?

时间:2017-09-13 21:26:39

标签: ef-code-first entity-framework-6 ef-migrations

我有一个现有索引(IX_ItemImportSummaryId),它是从ItemImportSummary上的ForeignKey生成的,因为这是默认的索引命名结构。如果我尝试在Id字段上显式创建具有相同名称的索引,它将删除索引,然后在迁移脚本中再次创建它。

有趣的是,如果我给它一个非默认名称,它只会重命名索引而不是删除然后创建它。在这种情况下,EF似乎很愚蠢,并没有意识到它根本不会做任何事情。

public class ItemImportSummaryDetail
{
    public int Id { get; set; }

    [ForeignKey("ItemImportSummaryId")]
    public virtual ItemImportSummary ItemImportSummary { get; set; }

    [Index("IX_ItemImportSummaryId", IsUnique = false)]
    [Index("IX_Mpbid", 2, IsUnique = true)]
    public int ItemImportSummaryId { get; set; }

    [Required]
    [StringLength(maximumLength:10)]
    [Index("IX_Mpbid", 1, IsUnique = true)]
    public string Mpbid { get; set; }
}

这是add-migration生成的内容:

DropIndex("dbo.ItemImportSummaryDetails", new[] { "ItemImportSummaryId" });
CreateIndex("dbo.ItemImportSummaryDetails", "ItemImportSummaryId");

0 个答案:

没有答案
相关问题