将 EF 迁移重置为全新状态 - 未生成唯一索引

Reset EF migrations to clean slate - unique index not generated

我正在尝试根据本指南将迁移重置为全新状态:

https://weblog.west-wind.com/posts/2016/Jan/13/Resetting-Entity-Framework-Migrations-to-a-clean-Slate

问题是一个数据库 table 包含一个唯一索引,该索引是在其中一个迁移中添加的

CreateIndex("Localization_Resources", new string[] { "Culture", "Key" }, unique: true, name: "UX_Localization_Resources_Culture_Key"); 

并且当我根据指南 运行 add-migration Initial 时,此索引不会重新生成到干净的初始数据迁移中。 为什么这个索引没有生成到初始迁移中?如何解决?谢谢。

如果它不是迁移的一部分,那么您必须使用 fluent API 手动添加唯一索引,如下例所示。

modelBuilder.Entity<YourEntity>()
            .HasIndex(b => b.PropertyName)
            .IsUnique();