sql-server - EF Code-First 迁移 ALTER TABLE DROP COLUMN Id 失败,因为一个或多个对象访问此列

标签 sql-server entity-framework entity-framework-migrations

我在更新数据库时遇到问题,当我应用迁移时出现此错误:

The object 'PK_dbo.CostCenter' is dependent on column 'Id'.
ALTER TABLE DROP COLUMN Id failed because one or more objects access this column.

我只是尝试向此表添加虚拟属性 (CostCenter),以便能够获取该数据

[Table("Department")]
public class Department
{
    public int Id { get; set; }

    public string Code { get; set; }

    public virtual IList<Cost> Costs_Department { get; set; }
    public virtual CostCenter CostCenter { get; set; }
}

这是 CostCenter 表

[Table("CostCenter")]
public class CostCenter
{
    public int Id { get; set; }

    public string Code { get; set; }

    [Required]
    public virtual Department Department { get; set; }
}

还有另一个与部门相关的表

[Table("Cost")]
public class Cost
{
    public int Id { get; set; }

    ...

    public virtual Department Departament { get; set; }
    public virtual Material Material { get; set; }
}

我添加的唯一更改是 Department 表上的 CostCenter 属性,这是 VS 在迁移文件上创建的内容

public partial class CC : DbMigration
{
    public override void Up()
    {
        DropForeignKey("dbo.CostCenter", "Department_CostCenter_Id", "dbo.Department");
        DropIndex("dbo.CostCenter", new[] { "Department_CostCenter_Id" });
        DropColumn("dbo.CostCenter", "Id");
        RenameColumn(table: "dbo.CostCenter", name: "Department_CostCenter_Id", newName: "Id");
        DropPrimaryKey("dbo.CostCenter");
        AlterColumn("dbo.CostCenter", "Id", c => c.Int(nullable: false));
        AddPrimaryKey("dbo.CostCenter", "Id");
        CreateIndex("dbo.CostCenter", "Id");
        AddForeignKey("dbo.CostCenter", "Id", "dbo.Department", "Id");
    }

    public override void Down()
    {
        DropForeignKey("dbo.CostCenter", "Id", "dbo.Department");
        DropIndex("dbo.CostCenter", new[] { "Id" });
        DropPrimaryKey("dbo.CostCenter");
        AlterColumn("dbo.CostCenter", "Id", c => c.Int(nullable: false, identity: true));
        AddPrimaryKey("dbo.CostCenter", "Id");
        RenameColumn(table: "dbo.CostCenter", name: "Id", newName: "Department_CostCenter_Id");
        AddColumn("dbo.CostCenter", "Id", c => c.Int(nullable: false, identity: true));
        CreateIndex("dbo.CostCenter", "Department_CostCenter_Id");
        AddForeignKey("dbo.CostCenter", "Department_CostCenter_Id", "dbo.Department", "Id", cascadeDelete: true);
    }
}

我在其他表上已经有类似的关系,所以我不知道这次是什么导致了问题

最佳答案

我遇到了类似的问题,只需删除所有以前的迁移文件,创建一个新文件并执行 update-database 命令。如果可以的话,也删除之前的数据库。

关于sql-server - EF Code-First 迁移 ALTER TABLE DROP COLUMN Id 失败,因为一个或多个对象访问此列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31507193/

相关文章:

SQL - 使用窗口和 case 语句展平表

asp.net-mvc - 如何在cshtml中使用下拉列表

c# - 使用 Entity Framework 在 View 上创建伪外键

entity-framework - 使用 Visual Studio 2013 的 Entity Framework 迁移错误

c# - MVC5 : UserManager. 添加到角色 () : "Error Adding User to Role: UserId not found"?

c# - 在 ASP.Net MVC 中合并多个数据库上下文

c++ - QSqlRelation 不起作用

sql-server - 嵌入在选择查询中的动态 SQL

sql - 从函数执行存储过程

c# - 有没有办法将外部函数内联到 EF Linq 查询中?