我更改了配置,這是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;
}
}
}
你可以在遷移之前和之後分享你的模型嗎?配置更改是什麼? – lukew
其相當大的模型,我將AdditionalCostLines的鏈接從派生類型更改爲基類型。 SQL完全相同,因爲配置是TPH,所以它使用相同的字段。 –
modelBuilder.Entity().HasMany(ac => ac.AdditionalCosts).WithRequired(o => o.Order).HasForeignKey(f => f.OrderID).WillCascadeOnDelete(false);曾經是WorkOrder而不是Order。 WorkOrder來自Order。 –