1

我更改了配置,這是EF6想出了EF遷移被複制報表

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

遷移爲什麼每個語句複製?

我使用實體框架版本6.

遷移本身也是廢人一個,因爲它刪除並重新創建相同的索引和外鍵,但我的背景已經改變,所以我必須插頁遷移記錄下運行的代碼。這裏生成的SQL,禮貌更新數據庫-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]) 

編輯:包含更多示例代碼:

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

你可以在遷移之前和之後分享你的模型嗎?配置更改是什麼? – lukew

+0

其相當大的模型,我將AdditionalCostLines的鏈接從派生類型更改爲基類型。 SQL完全相同,因爲配置是TPH,所以它使用相同的字段。 –

+0

modelBuilder.Entity ().HasMany(ac => ac.AdditionalCosts).WithRequired(o => o.Order).HasForeignKey(f => f.OrderID).WillCascadeOnDelete(false);曾經是WorkOrder而不是Order。 WorkOrder來自Order。 –

回答

2

雖然Store模式(即EF生成SQL)不會改變,因爲類改變了,EntityFramework看起來模型確實發生了變化,它需要支持遷移。您可能會收到重複的遷移,因爲該表是從您的模型中兩次引用的。既然您知道這是您的應用程序的NOP,那麼您可以安全地生成一個空的遷移,只更新存儲在數據庫中的快照。

從包管理器控制檯產生一個空遷移:

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

請記住,通過生成你是有前途的EF數據庫模式將在你的項目中的代碼匹配空遷移。

+0

我基本上做了同樣的事情,我評論了生成的代碼,所以發生的唯一SQL是遷移歷史記錄行插入。 –