无法先从模型代码中删除 FK
Unable to delete FK from model code-first
我已经使用现有数据库的代码优先方法创建了一个 MVC 应用程序。
我注意到我的 table 之一不需要外键,所以我尝试删除它,但我不断收到:
The object 'FK_BorrowedProperty_codeStatus' is dependent on column 'StatusId'.
ALTER TABLE DROP COLUMN StatusId failed because one or more objects access this column.
我完成的步骤
- 进入
BorrowedProperty
class 并删除 public int StatusId { get; set; }
& public virtual codeStatu codeStatu { get; set; }
- 进入
CodeStatu
class并删除[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<BorrowedProperty> BorrowedProperties { get; set; }
- 进入我的 DbContext 并注释掉这一行
//modelBuilder.Entity<codeStatu>()
// .HasMany(e => e.BorrowedProperties)
// .WithRequired(e => e.codeStatu)
// .HasForeignKey(e => e.StatusId)
// .WillCascadeOnDelete(false);
- 添加-迁移 DeleteStatusFKFromBorrowedProperty
这导致:
public partial class DeleteStatusFKFromBorrowedProperty : DbMigration
{
public override void Up()
{
DropForeignKey("dbo.BorrowedProperty", "StatusId", "dbo.codeStatus");
DropIndex("dbo.BorrowedProperty", new[] { "StatusId" });
DropColumn("dbo.BorrowedProperty", "StatusId");
}
public override void Down()
{
AddColumn("dbo.BorrowedProperty", "StatusId", c => c.Int(nullable: false));
CreateIndex("dbo.BorrowedProperty", "StatusId");
AddForeignKey("dbo.BorrowedProperty", "StatusId", "dbo.codeStatus", "Id");
}
}
- 更新数据库
然后我收到上面的错误。 BorrowedProperty 数据库 table.
中有 0 条记录
我做错了什么?
从异常消息中可以清楚地看出,您有名为 FK_BorrowedProperty_codeStatus
的外键约束,它可以防止删除 StatusId
列。您可以创建一个包含 SQL 命令的方法来删除 FK 约束,如下所示:
private void DropForeignKeyConstraint()
{
// This command may work if the constraint name is known:
// Sql(@"ALTER TABLE [dbo].[BorrowedProperty] DROP CONSTRAINT [FK_BorrowedProperty_codeStatus]");
Sql(@"DECLARE @constraint NVARCHAR(128)
SELECT @constraint = name
FROM sys.default_constraints
WHERE parent_object_id = object_id(N'dbo.BorrowedProperty')
AND col_name(parent_object_id, parent_column_id) = 'StatusId';
IF @constraint IS NOT NULL
EXECUTE('ALTER TABLE [dbo].[BorrowedProperty] DROP CONSTRAINT [' + @constraint + ']')");
}
将方法放在 Up
方法中,迁移代码如下:
public partial class DeleteStatusFKFromBorrowedProperty : DbMigration
{
public override void Up()
{
DropForeignKeyConstraint(); // dropping FK constraint first
DropForeignKey("dbo.BorrowedProperty", "StatusId", "dbo.codeStatus");
DropIndex("dbo.BorrowedProperty", new[] { "StatusId" });
DropColumn("dbo.BorrowedProperty", "StatusId"); // drop column at last
}
}
或者,如果您有权访问 SSMS 实例,运行 直接查询:
ALTER TABLE [dbo].[BorrowedProperty] DROP CONSTRAINT [FK_BorrowedProperty_codeStatus]
注意:您可以尝试注释掉 Down
方法中的所有代码行,以查看是否没有重新创建外键约束。
之后,您可以在程序包管理器控制台中执行 Add-Migration
& Update-Database
以应用上述更改。
类似问题:
The object 'DF__*' is dependent on column '*' - Changing int to double
How do I drop a column with object dependencies in SQL Server 2008?
Entity Framework Migrations can't drop table because of foreign key constraint
我已经使用现有数据库的代码优先方法创建了一个 MVC 应用程序。
我注意到我的 table 之一不需要外键,所以我尝试删除它,但我不断收到:
The object 'FK_BorrowedProperty_codeStatus' is dependent on column 'StatusId'. ALTER TABLE DROP COLUMN StatusId failed because one or more objects access this column.
我完成的步骤
- 进入
BorrowedProperty
class 并删除public int StatusId { get; set; }
&public virtual codeStatu codeStatu { get; set; }
- 进入
CodeStatu
class并删除[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")] public virtual ICollection<BorrowedProperty> BorrowedProperties { get; set; }
- 进入我的 DbContext 并注释掉这一行
//modelBuilder.Entity<codeStatu>() // .HasMany(e => e.BorrowedProperties) // .WithRequired(e => e.codeStatu) // .HasForeignKey(e => e.StatusId) // .WillCascadeOnDelete(false);
- 添加-迁移 DeleteStatusFKFromBorrowedProperty
这导致:
public partial class DeleteStatusFKFromBorrowedProperty : DbMigration
{
public override void Up()
{
DropForeignKey("dbo.BorrowedProperty", "StatusId", "dbo.codeStatus");
DropIndex("dbo.BorrowedProperty", new[] { "StatusId" });
DropColumn("dbo.BorrowedProperty", "StatusId");
}
public override void Down()
{
AddColumn("dbo.BorrowedProperty", "StatusId", c => c.Int(nullable: false));
CreateIndex("dbo.BorrowedProperty", "StatusId");
AddForeignKey("dbo.BorrowedProperty", "StatusId", "dbo.codeStatus", "Id");
}
}
- 更新数据库
然后我收到上面的错误。 BorrowedProperty 数据库 table.
中有 0 条记录我做错了什么?
从异常消息中可以清楚地看出,您有名为 FK_BorrowedProperty_codeStatus
的外键约束,它可以防止删除 StatusId
列。您可以创建一个包含 SQL 命令的方法来删除 FK 约束,如下所示:
private void DropForeignKeyConstraint()
{
// This command may work if the constraint name is known:
// Sql(@"ALTER TABLE [dbo].[BorrowedProperty] DROP CONSTRAINT [FK_BorrowedProperty_codeStatus]");
Sql(@"DECLARE @constraint NVARCHAR(128)
SELECT @constraint = name
FROM sys.default_constraints
WHERE parent_object_id = object_id(N'dbo.BorrowedProperty')
AND col_name(parent_object_id, parent_column_id) = 'StatusId';
IF @constraint IS NOT NULL
EXECUTE('ALTER TABLE [dbo].[BorrowedProperty] DROP CONSTRAINT [' + @constraint + ']')");
}
将方法放在 Up
方法中,迁移代码如下:
public partial class DeleteStatusFKFromBorrowedProperty : DbMigration
{
public override void Up()
{
DropForeignKeyConstraint(); // dropping FK constraint first
DropForeignKey("dbo.BorrowedProperty", "StatusId", "dbo.codeStatus");
DropIndex("dbo.BorrowedProperty", new[] { "StatusId" });
DropColumn("dbo.BorrowedProperty", "StatusId"); // drop column at last
}
}
或者,如果您有权访问 SSMS 实例,运行 直接查询:
ALTER TABLE [dbo].[BorrowedProperty] DROP CONSTRAINT [FK_BorrowedProperty_codeStatus]
注意:您可以尝试注释掉 Down
方法中的所有代码行,以查看是否没有重新创建外键约束。
之后,您可以在程序包管理器控制台中执行 Add-Migration
& Update-Database
以应用上述更改。
类似问题:
The object 'DF__*' is dependent on column '*' - Changing int to double
How do I drop a column with object dependencies in SQL Server 2008?
Entity Framework Migrations can't drop table because of foreign key constraint