首页 > 解决方案 > 将 EF 迁移重置为全新状态 - 未生成唯一索引

问题描述

我正在尝试根据本指南将迁移重置为干净的状态:

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

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

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

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

标签: entity-frameworkentity-framework-migrations

解决方案


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

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

推荐阅读