Reputation: 70028
We are a small team that uses Entity Framework as our object-relational mapping (ORM) framework. We use git as source control and in our latest sprint we have had database changes in two separate branches. Nothing out of the ordinary there.
However we can usually get a correct model snapshot by adding a blank "merge" migration.
However in our latest merge something really weird has happened. Our models and the database are out of sync. The given model below has a composite primary key of five properties.
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; }
}
In the database there are however a composite primary key of six columns. I know this has been edited but I can't find the migration for it now.
When I try to create a new migration it looks like this:
public partial class RemoveDueDatefromNotifiedEvent : DbMigration
{
public override void Up()
{
}
public override void Down()
{
}
}
If I add the DueDate key to the model again entity framework tries to add the existing key to the database again.
[Key, Column(Order = 6)]
public DateTime DueDate { get; set; }
Migration:
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" });
}
}
Upvotes: 1
Views: 66
Reputation: 70028
The migration was still there but during a conflict the projects .csproj
had not included the migration. When the migration was included again everything started working.
Upvotes: 1