Entity Framework code first database not synced with models

c# entity-framework entity-framework-6

Question

Our object-relational mapping (ORM) framework of choice for our small team is Entity Framework. We utilise Git for source control, and in our most recent sprint, two different branches had database updates. There is nothing unusual there.

However, by including a blank "merge" migration, we can often get an accurate model snapshot.

https://docs.microsoft.com/en-us/ef/ef6/modeling/code-first/migrations/teams#option-1-add-a-blank-merge-migration

However, something really strange occurred in our most recent merging. The database and our models are not in sync. A composite primary key of five characteristics is provided for the given model below.

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 am aware that this has been changed, however I am unable to locate the migration at this time.

enter image description here

This is what it looks like when I attempt to make a new migration:

public partial class RemoveDueDatefromNotifiedEvent : DbMigration
{
    public override void Up()
    {
    }

    public override void Down()
    {
    }
}

Entity framework wants to re-add the existing key to the database if I add the DueDate key to the model.

[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" });
    }
}
1
1
9/21/2018 4:23:07 PM

Accepted Answer

ZZZ_tmp
1
9/21/2018 3:42:30 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow