【发布时间】:2017-06-21 19:18:57
【问题描述】:
我已经使用代码优先方法和现有数据库创建了一个 MVC 应用程序。
我注意到我的一个表不需要外键,所以我试图删除它,但我不断收到:
对象“FK_BorrowedProperty_codeStatus”依赖于列“StatusId”。 ALTER TABLE DROP COLUMN StatusId 失败,因为一个或多个对象访问此列。
我已经完成的步骤
- 进入
BorrowedProperty类并删除public int StatusId { get; set; }和public virtual codeStatu codeStatu { get; set; } - 进入
CodeStatu类并删除[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 数据库表中有 零 条记录。
我做错了什么?
【问题讨论】:
标签: c# asp.net-mvc entity-framework entity-framework-migrations