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; } } }