如何从数据库 (ASP.NET MVC5) 中删除具有迁移约束的列
How can I remove a Column that has a Constraint with a migration from the DB (ASP.NET MVC5)
每次我尝试使用以下迁移删除列时:
public partial class RemoveProject_Id1InAspNetUsers2 : DbMigration
{
public override void Up()
{
DropColumn("dbo.AspNetUsers", "Project_Id1");
}
public override void Down()
{
}
}
我在 PMC 中 Update-Database
后出现以下错误:
The index 'IX_Project_Id1' is dependent on column 'Project_Id1'.
The object 'FK_dbo.AspNetUsers_dbo.Projects_Project_Id1' is dependent on column 'Project_Id1'.
ALTER TABLE DROP COLUMN Project_Id1 failed because one or more objects access this column.
这是我的 AspNetUser Table:
dbo.AspNetUser DB Image
这是我的项目Class:
public class Project
{
public int Id { get; set; }
[DisplayName("Project Manager")]
public string ProjectManagerId { get; set; }
// [ForeignKey("ProjectManagerId")]
// public ApplicationUser ProjectManager { get; set; }
//public string ProjectMembersId { get; set; }
//public List<ApplicationUser> ProjectMembers { get; set; }
public virtual ICollection<ProjectResource> OurProjectResources { get; set; }
}
我已经尝试从我的项目中移除所有对 ApplicationUser 的引用 Class。
PS:我已经尝试 运行 此 SQL 语句的迁移:
Sql("ALTER TABLE AspNetUsers DROP CONSTRAINT FK_AspNetUsers_Projects_Project_Id1");
并给出一个错误:
'FK_AspNetUsers_Projects_Project_Id1' is not a constraint.
Could not drop constraint. See previous errors.
我正在使用 EF6 Code First 工作流。
This post 解释了问题所在,但我想了解如何编写迁移以从数据库中删除列。
首先,您需要指定错误中的约束名称:
'FK_AspNetUsers_Projects_Project_Id1' is not a constraint. Could not
drop constraint. See previous errors.
因此,要删除约束,我们可以使用:
Sql("ALTER TABLE AspNetUsers DROP CONSTRAINT [FK_dbo.AspNetUsers_dbo.Projects_Project_Id1]");
然后,你得到错误,在字段上创建了一个索引:
The index 'IX_Project_Id1' is dependent on column 'Project_Id1'. ALTER
TABLE DROP COLUMN Project_Id1 failed because one or more objects
access this column.
我们也需要删除索引。
Sql("DROP INDEX [IX_Project_Id1] ON dbo.AspNetUsers ");
每次我尝试使用以下迁移删除列时:
public partial class RemoveProject_Id1InAspNetUsers2 : DbMigration
{
public override void Up()
{
DropColumn("dbo.AspNetUsers", "Project_Id1");
}
public override void Down()
{
}
}
我在 PMC 中 Update-Database
后出现以下错误:
The index 'IX_Project_Id1' is dependent on column 'Project_Id1'.
The object 'FK_dbo.AspNetUsers_dbo.Projects_Project_Id1' is dependent on column 'Project_Id1'.
ALTER TABLE DROP COLUMN Project_Id1 failed because one or more objects access this column.
这是我的 AspNetUser Table: dbo.AspNetUser DB Image
这是我的项目Class:
public class Project
{
public int Id { get; set; }
[DisplayName("Project Manager")]
public string ProjectManagerId { get; set; }
// [ForeignKey("ProjectManagerId")]
// public ApplicationUser ProjectManager { get; set; }
//public string ProjectMembersId { get; set; }
//public List<ApplicationUser> ProjectMembers { get; set; }
public virtual ICollection<ProjectResource> OurProjectResources { get; set; }
}
我已经尝试从我的项目中移除所有对 ApplicationUser 的引用 Class。
PS:我已经尝试 运行 此 SQL 语句的迁移:
Sql("ALTER TABLE AspNetUsers DROP CONSTRAINT FK_AspNetUsers_Projects_Project_Id1");
并给出一个错误:
'FK_AspNetUsers_Projects_Project_Id1' is not a constraint.
Could not drop constraint. See previous errors.
我正在使用 EF6 Code First 工作流。
This post 解释了问题所在,但我想了解如何编写迁移以从数据库中删除列。
首先,您需要指定错误中的约束名称:
'FK_AspNetUsers_Projects_Project_Id1' is not a constraint. Could not drop constraint. See previous errors.
因此,要删除约束,我们可以使用:
Sql("ALTER TABLE AspNetUsers DROP CONSTRAINT [FK_dbo.AspNetUsers_dbo.Projects_Project_Id1]");
然后,你得到错误,在字段上创建了一个索引:
The index 'IX_Project_Id1' is dependent on column 'Project_Id1'. ALTER TABLE DROP COLUMN Project_Id1 failed because one or more objects access this column.
我们也需要删除索引。
Sql("DROP INDEX [IX_Project_Id1] ON dbo.AspNetUsers ");