C# 如何从Dotnet EF中删除唯一索引?
我有一个模型: 使用者 我检查了我的MySQL数据库,发现ManagerId是唯一的。我没有把它设定为独一无二的。现在我想使用dotnet ef迁移来删除它。怎么做 目前我使用的是donet core 2.1 我已经测试了删除ManagerID和Manager并执行dotnet ef迁移添加UpdateUser 选中并删除ManagerID。然后我尝试再次添加新名称AssignedManagerID,它创建了一个新名称,其唯一性为trueC# 如何从Dotnet EF中删除唯一索引?,c#,asp.net,.net,entity-framework-core,ef-core-2.1,C#,Asp.net,.net,Entity Framework Core,Ef Core 2.1,我有一个模型: 使用者 我检查了我的MySQL数据库,发现ManagerId是唯一的。我没有把它设定为独一无二的。现在我想使用dotnet ef迁移来删除它。怎么做 目前我使用的是donet core 2.1 我已经测试了删除ManagerID和Manager并执行dotnet ef迁移添加UpdateUser 选中并删除ManagerID。然后我尝试再次添加新名称AssignedManagerID,它创建了一个新名称,其唯一性为true migrationBuilder.CreateIndex
migrationBuilder.CreateIndex(
name: "IX_Users_AssignedManagerId",
table: "Users",
column: "AssignedManagerId",
unique: true);
我的数据上下文:
公共数据库集用户{get;set;}
我对用户的OnModelCreating()一点也不流利您可以尝试下面的代码
migrationBuilder.DropIndex(name:"IX_Users_AssignedManagerId",table:"Users");
它在我的项目中起作用。谢谢EF Core被两个自引用的
用户
导航属性搞混了,错误地认为您试图在用户.Manager
和用户.Supervisor
之间建立一对一关系模型。它(随机)选择supervisor
作为主体,并通过ManagerId
创建唯一的FK关系<代码>至上属性不被视为FK,因此不会为其创建FK关系
通常,当EF核心假设不正确时,应使用数据注释和/或fluent API明确解决这些假设。对于自引用关系,唯一的选项是fluent API,因此将以下内容添加到您的OnModelCreating
覆盖中,以创建两个一对多关系:
modelBuilder.Entity<User>().HasOne(e => e.Manager).WithMany();
modelBuilder.Entity<User>().HasOne(e => e.Supervisor).WithMany();
modelBuilder.Entity().HasOne(e=>e.Manager).WithMany();
modelBuilder.Entity().HasOne(e=>e.Supervisor).WithMany();
默认情况下,Id
字段被视为唯一字段,应用迁移后您是否更改了模型?否,我没有更改任何内容。我检查SupervisorId不是唯一的。只有managerId。我不认为Id是唯一的,因为managerId不应该是唯一的。你能试试[Index(IsUnique=false)]
吗?还是流利的?
modelBuilder.Entity<User>().HasOne(e => e.Manager).WithMany();
modelBuilder.Entity<User>().HasOne(e => e.Supervisor).WithMany();