C# 重命名后的DbMigrations dooms表

C# 重命名后的DbMigrations dooms表,c#,sql,sql-server,entity-framework,C#,Sql,Sql Server,Entity Framework,我最近开始使用DbMigrations,因为我的项目离发布越来越近,似乎遇到了一个大问题 我的旧模型有两个实体: [Table("hist_history")] public partial class JobHistory { [Column("hist_id"), Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int Id { get; set; } [Column("hist_de

我最近开始使用DbMigrations,因为我的项目离发布越来越近,似乎遇到了一个大问题

我的旧模型有两个实体:

[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; }
}

我不能理解这个错误!约束存在,但名称已过期。有什么帮助吗?

有时候,在迁移过程中,如果希望渺茫,最好的办法就是创建一个新的迁移

您可以通过(备份)来完成此操作

  • 正在删除迁移文件夹中的所有内容
  • 将连接字符串指向新数据库(数据库中尚不存在的数据库)
  • 在包管理器中添加新迁移(添加迁移)
  • 运行更新数据库以创建新的DB&migrations表
  • 现在,您所做的一切都将捆绑到一个迁移中,而不是几个迁移中

    如果您已经有一个包含所有测试数据的数据库,那么可以擦除迁移表,并将其更新为与刚刚创建的数据库相匹配。现在,如果返回数据库,可以继续使用新模式

    这应该确保您的模式和迁移是正确的,并且您不应该在对模型所做的更改方面再遇到任何异常错误

    此问题有时是由手动迁移、自动迁移以及有时与独立于您的开发团队创建迁移的团队合作的混合造成的。我的经验法则是避免所有自动迁移,并尝试在每个版本中保持一次迁移


    为此,请删除在开发过程中创建的任何迁移,并在准备部署时生成一个新的迁移。使用这种方法,您的迁移文件夹每个版本应该有一次迁移。

    谢谢您的建议。我已经关闭了自动迁移,在做了一些修改之后,我的测试数据库接受了我按照您所说的重新创建的手动迁移。如何捆绑发布迁移?你是否有
    #if!调试
    标记类并确保在发布时重新生成?抱歉,我使用了bundle这个词,但我的实际意思是:当我开发系统或更新网站时,我可能会更改模式/模型几次,创建几次迁移。当我完成并希望第一次启用站点或发布更新时,我会删除为更改创建的所有迁移,并创建一个新迁移。这样,如果您查看migrations文件夹,每个生产版本应该有一次迁移。有点误导,抱歉,但这正是我真正的意思。
    [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");
        }
    }
    
    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.