1

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; } } } 
5
  • Could you share your model before and after the migration? what was the configuration change? Commented Feb 11, 2014 at 23:56
  • its quite a big model, I changed the link for AdditionalCostLines from a derived type to a base type. The SQL is exactly the same, because the configuration is TPH so it uses the same fields. Commented Feb 12, 2014 at 22:41
  • modelBuilder.Entity<Order>().HasMany(ac => ac.AdditionalCosts).WithRequired(o => o.Order).HasForeignKey(f => f.OrderID).WillCascadeOnDelete(false); used to be WorkOrder in place of Order. WorkOrder is derived from Order. Commented Feb 12, 2014 at 22:42
  • A minimal repro would be really helpful to determine the root cause of this issue. Possibly code only containing AdditioanlCostLines, Documents, Orders, and WorkOrders if that is possible. Commented Feb 28, 2014 at 0:11
  • @lukew I added some more code Commented Feb 28, 2014 at 17:48

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I essentially did the same thing, I commented out the produced code so the only SQL that happened was the migrations history row insertion.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.