【发布时间】:2018-09-21 15:34:33
【问题描述】:
我们是一个使用实体框架作为我们的对象关系映射 (ORM) 框架的小团队。我们使用 git 作为源代码控制,在我们最近的 sprint 中,我们在两个独立的分支中进行了数据库更改。那里没有什么不寻常的地方。
但是,我们通常可以通过添加空白“合并”迁移来获得正确的模型快照。
然而,在我们最近的合并中,发生了一些非常奇怪的事情。我们的模型和数据库不同步。下面给定的模型具有五个属性的复合主键。
public class NotifiedEvent
{
[Key, Column(Order = 0)]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
[MaxLength(15)]
public string BusinessSystemId { get; set; }
[Key, Column(Order = 1)]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int CaseId { get; set; }
[Key, Column(Order = 2)]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
[MaxLength(5)]
public string Action { get; set; }
[Key, Column(Order = 3)]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int Cycle { get; set; }
[ForeignKey("BusinessSystemId,CaseId,Action,Cycle")]
public virtual TPRenewalCycle TPRenewalCycle { get; set; }
[Key, Column(Order = 4)]
[ForeignKey("TPEvent")]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int EventNo { get; set; }
public virtual TPEvent TPEvent { get; set; }
[ForeignKey("BusinessSystemId,CaseId,Action,Cycle,EventNo")]
public virtual TPCaseEvent TpCaseEvent { get; set; }
public int NotifiedBatchId { get; set; }
public virtual NotifiedBatch NotifiedBatch { get; set; }
[Key, Column(Order = 5)]
public DateTime EventDate { get; set; }
public DateTime Created { get; set; }
public DateTime Updated { get; set; }
}
然而,在数据库中有一个由六列组成的复合主键。我知道这已被编辑,但我现在找不到它的迁移。
当我尝试创建一个新的迁移时,它看起来像这样:
public partial class RemoveDueDatefromNotifiedEvent : DbMigration
{
public override void Up()
{
}
public override void Down()
{
}
}
如果我再次将 DueDate 键添加到模型中,实体框架会尝试再次将现有键添加到数据库中。
[Key, Column(Order = 6)]
public DateTime DueDate { get; set; }
迁移:
public partial class RemovedDueDatefromNotifiedEvent : DbMigration
{
public override void Up()
{
DropPrimaryKey("dbo.NotifiedEvents");
AddColumn("dbo.NotifiedEvents", "DueDate", c => c.DateTime(nullable: false, precision: 0, storeType: "datetime2"));
AddPrimaryKey("dbo.NotifiedEvents", new[] { "BusinessSystemId", "CaseId", "Action", "Cycle", "EventNo", "EventDate", "DueDate" });
}
public override void Down()
{
DropPrimaryKey("dbo.NotifiedEvents");
DropColumn("dbo.NotifiedEvents", "DueDate");
AddPrimaryKey("dbo.NotifiedEvents", new[] { "BusinessSystemId", "CaseId", "Action", "Cycle", "EventNo", "EventDate" });
}
}
【问题讨论】:
标签: c# entity-framework entity-framework-6