Entity Framework code first database not synced with models

c# entity-framework entity-framework-6

Question

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.

https://docs.microsoft.com/en-us/ef/ef6/modeling/code-first/migrations/teams#option-1-add-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.

enter image description here

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

Accepted Answer

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.

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