Q:

DbMigrations dooms table after renaming

I recently started using DbMigrations as my project gets closer to release, and seem to have run into a major problem.
My old model had these two entities:
[Table("hist_history")]
public partial class JobHistory
{
    [Column("hist_id"), Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }

    [Column("hist_description"), MaxLength(CommonEntityValues.StringLineLength)]
    public string Description { get; set; }

    [Column("jobr_id")]
    public int JobId { get; set; }

    [ForeignKey("JobId"), InverseProperty("History")]
    public virtual Job Job { get; set; }

    [Column("joht_id")]
    public int TypeId { get; set; }

    [ForeignKey("TypeId"), InverseProperty("History")]
    public virtual JobHistoryType Type { get; set; }
}

[Table("joht_job_history_type")]
public partial class JobHistoryType
{
    [Column("joht_id"), Key, DatabaseGenerated(DatabaseGeneratedOption.None)]
    public int Id { get; set; }

    [Column("joht_name"), Required, Index(IsUnique = true), MaxLength(CommonEntityValues.StringLongReferenceLength)]
    public string Name { get; set; }

    [InverseProperty("Type")]
    public virtual ICollection<JobHistory> History { get; set; }
}

They were later updated and migration created:
[Table("hist_history")]
public partial class History
{
    [Column("hist_id"), Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }

    [Column("hist_description"), MaxLength(CommonEntityValues.StringLineLength)]
    public string Description { get; set; }

    [Column("jobr_id")]
    public int JobId { get; set; }

    [ForeignKey("JobId"), InverseProperty("History")]
    public virtual Job Job { get; set; }

    [Column("hisy_id")]
    public int TypeId { get; set; }

    [ForeignKey("TypeId"), InverseProperty("History")]
    public virtual HistoryType Type { get; set; }
}

[Table("hisy_history_type")]
public partial class HistoryType
{
    [Column("hisy_id"), Key, DatabaseGenerated(DatabaseGeneratedOption.None)]
    public int Id { get; set; }

    [Column("hisy_name"), Required, Index(IsUnique = true), MaxLength(CommonEntityValues.StringLongReferenceLength)]
    public string Name { get; set; }

    [InverseProperty("Type")]
    public virtual ICollection<History> History { get; set; }
}

public partial class RenamedJobHistoryType : DbMigration
{
    public override void Up()
    {
        RenameTable(name: "dbo.joht_job_history_type", newName: "hisy_history_type");
        RenameColumn(table: "dbo.hist_history", name: "joht_id", newName: "hisy_id");
        RenameColumn(table: "dbo.hisy_history_type", name: "joht_id", newName: "hisy_id");
        RenameColumn(table: "dbo.hisy_history_type", name: "joht_name", newName: "hisy_name");
        RenameIndex(table: "dbo.hist_history", name: "IX_joht_id", newName: "IX_hisy_id");
    }

    public override void Down()
    {
        RenameIndex(table: "dbo.hist_history", name: "IX_hisy_id", newName: "IX_joht_id");
        RenameColumn(table: "dbo.hisy_history_type", name: "hisy_name", newName: "joht_name");
        RenameColumn(table: "dbo.hisy_history_type", name: "hisy_id", newName: "joht_id");
        RenameColumn(table: "dbo.hist_history", name: "hisy_id", newName: "joht_id");
        RenameTable(name: "dbo.hisy_history_type", newName: "joht_job_history_type");
    }
}

Now, however. All of my future migrations (and automatic ones) throw this error:
The constraint 'PK_dbo.hisy_history_type' is being referenced by table 'hist_history', foreign key constraint 'FK_dbo.hist_history_dbo.joht_job_history_type_joht_id'.
Could not drop constraint. See previous errors.

I cannot make sense of this error! The constraint exists, although the name is out of date. Any help please?

A:

Sometimes with migrations if you are running out of hope the best thing you can do is create a fresh migration.
You can do this by (take backups)

Removing everything from your migrations folder
Point your connection string at a new database (that doesn't yet exist in your DB)
Add a new migration in the package manager (Add-Migration)
Run Update-Database to create a new DB & migrations table

Now everything you've done will be bundled into one migration, rather than several migrations.
If you already had a database with all your test data in it, you can wipe the migrations table, and update it to match the database you've just created. Now if you point back to your database you can continue with the fresh schema.
This should ensure that your schema and migrations are correct, and you shouldn't run into any further unusual errors regarding a change you've made to your model.
This issue is sometimes caused by a mixture of manual migrations, automatic migrations and sometimes working with a team who are creating migrations independent of your development. My rule of thumb is to avoid automatic migrations all together, and try to keep one migrations per release. 
Do this by deleting any migrations created during development and generating a single fresh migration when ready for deployment. Using this approach your migration folder should then have one migration per release.

