数据库迁移失败使状态不一致的数据库迁移失败 [英] Failed database migration left database in inconsistent state migrations failing

查看:54
本文介绍了数据库迁移失败使状态不一致的数据库迁移失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下3个模型,这些模型在进行一些更改时使我处于数据库状态不一致的状态,因为由于删除了失败的迁移并回滚到了以前的版本,我无法在表上取消约束.

I have the following 3 models which during some changes have left me with an inconsistent database state as I am unable to drop constraints on the table due to the deletion of a failed migration and the rollback to a previous version.

public class Bill
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.None)]
        public int BillID { get; set; }

        ..........

        public int? LandlordID { get; set; }
        public virtual Landlord Landlord { get; set; }

        public int? LandlordBillID { get; set; }
        public virtual LandlordBill LandlordBill { get; set; }

        public virtual ICollection<Meter> Meters { get; set; }

        public virtual ICollection<Lift> Lifts { get; set; }

        public virtual ICollection<Refund> Refunds { get; set; }
    }

public class HalfHourlyBill
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.None)]
        public int BillID { get; set; }

        ...........

        public int? LandlordID { get; set; }
        public virtual Landlord Landlord { get; set; }

        public int? LandlordBillID { get; set; }
        public virtual LandlordBill LandlordBill { get; set; }

    }

 public class LandlordBill
    {
        [Key]
        public int LandlordBillID { get; set; }

        .........

        public virtual ICollection<Bill> Bills { get; set; }

        public virtual ICollection<HalfHourlyBill> HalfHourlyBills { get; set; }

        public int? LandlordID { get; set; }
        public virtual Landlord Landlord { get; set; }
    }

我不确定如何才能使代码恢复为与数据库相同的状态,因为最近的更改使事情变得混乱,下面显示的迁移代码也失败了:

I am unsure as to how I can get back my code to be in the same state as my database as the last changes have messed this up and my migration code shown below just fails:

public override void Up()
        {
            DropForeignKey("dbo.Lift", "LandlordBill_LandlordBillID", "dbo.LandlordBill");
            DropForeignKey("dbo.Meter", "LandlordBill_LandlordBillID", "dbo.LandlordBill");
            DropForeignKey("dbo.Refund", "LandlordBill_LandlordBillID", "dbo.LandlordBill");
            DropForeignKey("dbo.Bill", "LandlordID", "dbo.Landlord");
            DropForeignKey("dbo.HalfHourlyBill", "LandlordID", "dbo.Landlord");
            DropForeignKey("dbo.Bill", "LandlordBillID", "dbo.LandlordBill");
            DropForeignKey("dbo.HalfHourlyBill", "LandlordBillID", "dbo.LandlordBill");
            DropIndex("dbo.Bill", new[] { "LandlordID" });
            DropIndex("dbo.Bill", new[] { "LandlordBillID" });
            DropIndex("dbo.Lift", new[] { "LandlordBill_LandlordBillID" });
            DropIndex("dbo.Meter", new[] { "LandlordBill_LandlordBillID" });
            DropIndex("dbo.Refund", new[] { "LandlordBill_LandlordBillID" });
            DropIndex("dbo.HalfHourlyBill", new[] { "LandlordID" });
            DropIndex("dbo.HalfHourlyBill", new[] { "LandlordBillID" });
            DropPrimaryKey("dbo.LandlordBill");
            AddColumn("dbo.Bill", "Landlord_LandlordID", c => c.Guid());
            AddColumn("dbo.LandlordBill", "LandlordID", c => c.Int());
            AddColumn("dbo.HalfHourlyBill", "Landlord_LandlordID", c => c.Guid());
            AlterColumn("dbo.Bill", "LandlordID", c => c.Int());
            AlterColumn("dbo.Bill", "LandlordBillID", c => c.Int());
            AlterColumn("dbo.LandlordBill", "LandlordBillID", c => c.Int(nullable: false, identity: true));
            AlterColumn("dbo.HalfHourlyBill", "LandlordID", c => c.Int());
            AlterColumn("dbo.HalfHourlyBill", "LandlordBillID", c => c.Int());
            AddPrimaryKey("dbo.LandlordBill", "LandlordBillID");
            CreateIndex("dbo.Bill", "LandlordBillID");
            CreateIndex("dbo.Bill", "Landlord_LandlordID");
            CreateIndex("dbo.HalfHourlyBill", "LandlordBillID");
            CreateIndex("dbo.HalfHourlyBill", "Landlord_LandlordID");
            AddForeignKey("dbo.Bill", "Landlord_LandlordID", "dbo.Landlord", "LandlordID");
            AddForeignKey("dbo.HalfHourlyBill", "Landlord_LandlordID", "dbo.Landlord", "LandlordID");
            AddForeignKey("dbo.Bill", "LandlordBillID", "dbo.LandlordBill", "LandlordBillID");
            AddForeignKey("dbo.HalfHourlyBill", "LandlordBillID", "dbo.LandlordBill", "LandlordBillID");
            DropColumn("dbo.Lift", "LandlordBill_LandlordBillID");
            DropColumn("dbo.Meter", "LandlordBill_LandlordBillID");
            DropColumn("dbo.Refund", "LandlordBill_LandlordBillID");
        }

        public override void Down()
        {
            AddColumn("dbo.Refund", "LandlordBill_LandlordBillID", c => c.Guid());
            AddColumn("dbo.Meter", "LandlordBill_LandlordBillID", c => c.Guid());
            AddColumn("dbo.Lift", "LandlordBill_LandlordBillID", c => c.Guid());
            DropForeignKey("dbo.HalfHourlyBill", "LandlordBillID", "dbo.LandlordBill");
            DropForeignKey("dbo.Bill", "LandlordBillID", "dbo.LandlordBill");
            DropForeignKey("dbo.HalfHourlyBill", "Landlord_LandlordID", "dbo.Landlord");
            DropForeignKey("dbo.Bill", "Landlord_LandlordID", "dbo.Landlord");
            DropIndex("dbo.HalfHourlyBill", new[] { "Landlord_LandlordID" });
            DropIndex("dbo.HalfHourlyBill", new[] { "LandlordBillID" });
            DropIndex("dbo.Bill", new[] { "Landlord_LandlordID" });
            DropIndex("dbo.Bill", new[] { "LandlordBillID" });
            DropPrimaryKey("dbo.LandlordBill");
            AlterColumn("dbo.HalfHourlyBill", "LandlordBillID", c => c.Guid());
            AlterColumn("dbo.HalfHourlyBill", "LandlordID", c => c.Guid());
            AlterColumn("dbo.LandlordBill", "LandlordBillID", c => c.Guid(nullable: false));
            AlterColumn("dbo.Bill", "LandlordBillID", c => c.Guid());
            AlterColumn("dbo.Bill", "LandlordID", c => c.Guid());
            DropColumn("dbo.HalfHourlyBill", "Landlord_LandlordID");
            DropColumn("dbo.LandlordBill", "LandlordID");
            DropColumn("dbo.Bill", "Landlord_LandlordID");
            AddPrimaryKey("dbo.LandlordBill", "LandlordBillID");
            CreateIndex("dbo.HalfHourlyBill", "LandlordBillID");
            CreateIndex("dbo.HalfHourlyBill", "LandlordID");
            CreateIndex("dbo.Refund", "LandlordBill_LandlordBillID");
            CreateIndex("dbo.Meter", "LandlordBill_LandlordBillID");
            CreateIndex("dbo.Lift", "LandlordBill_LandlordBillID");
            CreateIndex("dbo.Bill", "LandlordBillID");
            CreateIndex("dbo.Bill", "LandlordID");
            AddForeignKey("dbo.HalfHourlyBill", "LandlordBillID", "dbo.LandlordBill", "LandlordBillID");
            AddForeignKey("dbo.Bill", "LandlordBillID", "dbo.LandlordBill", "LandlordBillID");
            AddForeignKey("dbo.HalfHourlyBill", "LandlordID", "dbo.Landlord", "LandlordID");
            AddForeignKey("dbo.Bill", "LandlordID", "dbo.Landlord", "LandlordID");
            AddForeignKey("dbo.Refund", "LandlordBill_LandlordBillID", "dbo.LandlordBill", "LandlordBillID");
            AddForeignKey("dbo.Meter", "LandlordBill_LandlordBillID", "dbo.LandlordBill", "LandlordBillID");
            AddForeignKey("dbo.Lift", "LandlordBill_LandlordBillID", "dbo.LandlordBill", "LandlordBillID");
        }

我在迁移更新期间遇到的错误是表'HalfHourlyBill'引用了约束'PK_dbo.LandlordBill',外键约束'FK_dbo.HalfHourlyBill_dbo.LandlordBill_LandlordBillID'.无法删除约束.请参阅先前的错误.

The error that I am getting during the update of the migration is The constraint 'PK_dbo.LandlordBill' is being referenced by table 'HalfHourlyBill', foreign key constraint 'FK_dbo.HalfHourlyBill_dbo.LandlordBill_LandlordBillID'. Could not drop constraint. See previous errors.

推荐答案

在此处输入代码我也遇到了类似的麻烦.有时,您可以注释掉不需要运行的命令或重新排列命令,但这是我会做的:

enter code hereI had a similar mess. Sometimes you can comment out commands that don't need to be run or rearrange the commands, but here is what I would do:

1)为该迁移生成脚本

1) Generate a script for that migration

update-database -Script

2)这应该向您显示将要运行的命令.将该脚本粘贴到SQL Management Studio中,并逐步执行以查看已应用的内容和未应用的内容.

2) That should show you the commands that will be run. Paste that script into SQL Management Studio and step through them to see what has been applied, and what hasn't.

3)最后一条命令将更新__MigrationHistory以反映所应用的迁移.您可以运行它,也可以仅注释掉迁移Up()代码并执行更新数据库.

3) The last command will update the __MigrationHistory to reflect the applied migration. You can run it, or you can just comment out the migration Up() code and do update-database.

如果您相信一切都是同步的,则可以注释掉Up()代码并应用它,也可以创建无代码迁移来同步数据库和代码模型:

If you trust things are in sync you could just comment out the Up() code and apply it or you can create a no-code migration to syncronize your database and code models:

添加迁移resyncModels -IgnoreChanges .

这篇关于数据库迁移失败使状态不一致的数据库迁移失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆