【问题标题】:Cannot update database because entity cannot drop constraint无法更新数据库,因为实体无法删除约束
【发布时间】:2014-05-06 20:56:19
【问题描述】:

我创建了一些新表,但我注意到我没有为 id 使用良好的命名约定。

所以我用新的 id 做了一个新的add-migration,但是当我尝试update-database 时出现以下错误:

表正在引用约束“PK_dbo.DB_User_Type” 'AspNetUsers',外键约束 'FK_dbo.AspNetUsers_dbo.DB_User_Type_DB_User_Type_Id'。无法掉落 约束。查看以前的错误。

我不明白,因为脚本从删除所有约束开始。

有人能解释一下如何解决这个错误吗?

我正在使用实体 6.1 中的新方法 code first from an existing database。这是Up 函数

public override void Up()
        {
            DropForeignKey("dbo.DB_Company_Profile", "DB_Category_Id", "dbo.DB_Category");
            DropForeignKey("dbo.DB_Category_Translation", "DB_Category_Id", "dbo.DB_Category");
            DropForeignKey("dbo.DB_User_Type_Translation", "DB_User_Type_Id", "dbo.DB_User_Type");
            DropForeignKey("dbo.DB_Company_Profile", "category_id", "dbo.DB_Category");
            DropForeignKey("dbo.DB_Category_Translation", "category_id", "dbo.DB_Category");
            DropForeignKey("dbo.AspNetUsers", "DB_User_Type_user_type_id", "dbo.DB_User_Type");
            DropForeignKey("dbo.DB_User_Type_Translation", "user_type_id", "dbo.DB_User_Type");
            DropIndex("dbo.DB_Company_Profile", new[] { "DB_Category_Id" });
            DropIndex("dbo.DB_Category_Translation", new[] { "DB_Category_Id" });
            DropIndex("dbo.DB_User_Type_Translation", new[] { "DB_User_Type_Id" });
            DropColumn("dbo.DB_Company_Profile", "category_id");
            DropColumn("dbo.DB_Category_Translation", "category_id");
            DropColumn("dbo.DB_User_Type_Translation", "user_type_id");
            RenameColumn(table: "dbo.AspNetUsers", name: "DB_User_Type_Id", newName: "DB_User_Type_user_type_id");
            RenameColumn(table: "dbo.DB_Company_Profile", name: "DB_Category_Id", newName: "category_id");
            RenameColumn(table: "dbo.DB_Category_Translation", name: "DB_Category_Id", newName: "category_id");
            RenameColumn(table: "dbo.DB_User_Type_Translation", name: "DB_User_Type_Id", newName: "user_type_id");
            RenameIndex(table: "dbo.AspNetUsers", name: "IX_DB_User_Type_Id", newName: "IX_DB_User_Type_user_type_id");
            DropPrimaryKey("dbo.DB_Category");
            DropPrimaryKey("dbo.DB_User_Type");
            AddColumn("dbo.DB_Category", "category_id", c => c.Int(nullable: false, identity: true));
            AddColumn("dbo.DB_User_Type", "user_type_id", c => c.Int(nullable: false, identity: true));
            AlterColumn("dbo.DB_Company_Profile", "category_id", c => c.Int(nullable: false));
            AlterColumn("dbo.DB_Category_Translation", "category_id", c => c.Int(nullable: false));
            AlterColumn("dbo.DB_User_Type_Translation", "user_type_id", c => c.Int(nullable: false));
            AddPrimaryKey("dbo.DB_Category", "category_id");
            AddPrimaryKey("dbo.DB_User_Type", "user_type_id");
            CreateIndex("dbo.DB_Company_Profile", "category_id");
            CreateIndex("dbo.DB_Category_Translation", "category_id");
            CreateIndex("dbo.DB_User_Type_Translation", "user_type_id");
            AddForeignKey("dbo.DB_Company_Profile", "category_id", "dbo.DB_Category", "category_id", cascadeDelete: true);
            AddForeignKey("dbo.DB_Category_Translation", "category_id", "dbo.DB_Category", "category_id", cascadeDelete: true);
            AddForeignKey("dbo.DB_User_Type_Translation", "user_type_id", "dbo.DB_User_Type", "user_type_id", cascadeDelete: true);
            AddForeignKey("dbo.AspNetUsers", "DB_User_Type_user_type_id", "dbo.DB_User_Type", "user_type_id");
            DropColumn("dbo.DB_Category", "Id");
            DropColumn("dbo.DB_User_Type", "Id");
        }

【问题讨论】:

  • 如果你没有数据,最干净的方法是删除数据库>
  • 然后它说它不存在。我想我必须做的不是更新数据库
  • 我想我可以针对另一个迁移,但我想知道是否有一些不那么复杂的事情
  • @DarthVader 你知道除了修改数据库并制作新模型之外还有其他方法吗?

标签: c# entity-framework


【解决方案1】:

我遇到了同样的问题。 This post helped me.

问题是 EF 没有自动重命名我的FK_CONSTRAINST。结果,DropForeignKey() 不正确。您可以通过手动 SQL 查询来解决这个问题(查看上面的帖子)。但我通过首先在数据库中手动更改FK_CONSTRAINST 解决了这个问题。请记住,您可以使用-Verbose 来检查查询。更容易调试。

【讨论】:

    【解决方案2】:

    在运行update-database 命令之前,我必须手动删除外键。因为我的数据库中的约束名称类似于:FK_dbo.AspNetUsers_dbo.DB_User_Type_DB_User_TypeId

    虽然脚本试图删除的约束是: FK_dbo.AspNetUsers_dbo.DB_User_Type_DB_User_Type_Id

    【讨论】:

      【解决方案3】:

      嗯,有一些关于外键的规则——其中一些是为了处理一些情况,当您尝试删除一条记录,而该记录与它相关联的另一条记录时。 在您的情况下,您似乎想将CASCADE 规则应用于您的关系。这样实体将删除其所有 FK

      【讨论】:

      • 如果没有数据出现这个错误正常吗?
      • 你能提供一个实体的例子吗?
      • 为了进行CASCADE,需要修改建立PK-FK关系的表。您需要修改约束本身。
      【解决方案4】:
      1. DropForeignKey("dependantTable", "dependantColumn", "principalTable")
      2. DropPrimaryKey("principalTable")
      3. AddPrimaryKey("principalTable", "principalColumn",)
      4. AddForeignKey("dependantTable", "dependantColumn", "principalTable")

      【讨论】:

        猜你喜欢
        • 2017-09-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-12-27
        • 1970-01-01
        • 2020-01-05
        相关资源
        最近更新 更多