Question

I made a configuration change and this is the migration that EF6 came up with

 public override void Up()
    {
        DropForeignKey("dbo.AdditionalCostLines", "OrderID", "dbo.Documents");
        DropForeignKey("dbo.AdditionalCostLines", "OrderID", "dbo.Documents");
        DropIndex("dbo.AdditionalCostLines", new[] { "OrderID" });
        DropIndex("dbo.AdditionalCostLines", new[] { "OrderID" });
        CreateIndex("dbo.AdditionalCostLines", "OrderID");
        CreateIndex("dbo.AdditionalCostLines", "OrderID");
        AddForeignKey("dbo.AdditionalCostLines", "OrderID", "dbo.Documents", "ID");
        AddForeignKey("dbo.AdditionalCostLines", "OrderID", "dbo.Documents", "ID");
    }

    public override void Down()
    {
        DropForeignKey("dbo.AdditionalCostLines", "OrderID", "dbo.Documents");
        DropForeignKey("dbo.AdditionalCostLines", "OrderID", "dbo.Documents");
        DropIndex("dbo.AdditionalCostLines", new[] { "OrderID" });
        DropIndex("dbo.AdditionalCostLines", new[] { "OrderID" });
        CreateIndex("dbo.AdditionalCostLines", "OrderID");
        CreateIndex("dbo.AdditionalCostLines", "OrderID");
        AddForeignKey("dbo.AdditionalCostLines", "OrderID", "dbo.Documents", "ID", cascadeDelete: true);
        AddForeignKey("dbo.AdditionalCostLines", "OrderID", "dbo.Documents", "ID", cascadeDelete: true);

Why is every statement duplicated?

I'm using Entity Framework version 6.

The migration itself is also a useless one, as it drops and recreates the same indexes and foreign keys, but my context has changed so I have to inset a migration record for the code to run. Here the generated SQL, courtesy of update-database -script:

IF object_id(N'[dbo].[FK_dbo.AdditionalCostLines_dbo.Documents_OrderID]', N'F') IS NOT NULL
ALTER TABLE [dbo].[AdditionalCostLines] DROP CONSTRAINT         [FK_dbo.AdditionalCostLines_dbo.Documents_OrderID]
IF EXISTS (SELECT name FROM sys.indexes WHERE name = N'IX_OrderID' AND object_id = object_id(N'[dbo].[AdditionalCostLines]', N'U'))
DROP INDEX [IX_OrderID] ON [dbo].[AdditionalCostLines]
CREATE INDEX [IX_OrderID] ON [dbo].[AdditionalCostLines]([OrderID])
ALTER TABLE [dbo].[AdditionalCostLines] ADD CONSTRAINT [FK_dbo.AdditionalCostLines_dbo.Documents_OrderID] FOREIGN KEY ([OrderID]) REFERENCES [dbo].[Documents] ([ID])

EDIT: Include more sample code:

  public class Document
{
[Key]
public int ID { get; set; }
public virtual Vendor Vendor { get; set; }
[ForeignKey("Vendor")]
public int? VendorID { get; set; }

[Display(Name = "Reference Number")]
public string ReferenceNumber { get; set; }
public string Notes { get; set; }

public virtual List<OrderLine> Lines { get; set; }
}

public class Order : Document
{
    //Fields Atop Documents base fields for order style documents.


    [Display(Name = "Payment Terms")]
    public virtual PaymentTerms PaymentTerms { get; set; }
    [ForeignKey("PaymentTerms")]
    public int? PaymentTermsID { get; set; }

    //Collections

    public virtual List<AdditionalCostLine> AdditionalCosts { get; set; }
}

public class WorkOrder : Order
{
   [Display(Name = "Est. Ship Date")]
   [DataType(DataType.Date), DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}", ApplyFormatInEditMode = true)]
   public DateTime? EstimatedShipDate { get; set; }

public virtual List<WorkOrderLineChange> LineChanges { get; set; }
}

public class AdditionalCostLine
{
    [Key]
    public int ID { get; set; }

    public virtual Order Order { get; set; }
    [ForeignKey("Order")]
    public int OrderID { get; set; }

    public string Description { get; set; }

    [DisplayFormat(DataFormatString = "{0:$0.00#}")]
    public decimal Rate { get; set; }
    public int Quantity { get; set; }

    public virtual AdditionalCostType AdditionalCostType { get; set; }
    [ForeignKey("AdditionalCostType")]
    public int AdditionalCostTypeID { get; set; }

    [NotMapped]
    public decimal Amount
    {
        get
        {
            return Rate * Quantity;
        }
    }
}
Was it helpful?

Solution

While the Store model (the sql that EF generates) doesn't change, since the classes changed it appears to EntityFramework that the model did change and it needs to scaffold a migration. You are likely getting duplicate migrations because that table is referenced from your model twice. Since you know that this is a NOP for your application it is safe for you to generate an empty migration that simply updates the snapshot stored in your database.

To generate an empty migration from the package manager console:

PM> Add-Migration emptyMigration -IgnoreChanges
PM> Update-Database

Keep in mind that by generating an empty migration you are promising EF that the database model will match the code in your project.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top