C# 重命名后的DbMigrations dooms表
我最近开始使用DbMigrations,因为我的项目离发布越来越近,似乎遇到了一个大问题 我的旧模型有两个实体: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
[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; }
}
我不能理解这个错误!约束存在,但名称已过期。有什么帮助吗?有时候,在迁移过程中,如果希望渺茫,最好的办法就是创建一个新的迁移 您可以通过(备份)来完成此操作
为此,请删除在开发过程中创建的任何迁移,并在准备部署时生成一个新的迁移。使用这种方法,您的迁移文件夹每个版本应该有一次迁移。谢谢您的建议。我已经关闭了自动迁移,在做了一些修改之后,我的测试数据库接受了我按照您所说的重新创建的手动迁移。如何捆绑发布迁移?你是否有
#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.