我进行了配置更改,这是EF6提出的迁移
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);
为什么每个陈述都重复?
我正在使用Entity Framework第6版。
迁移本身也是一个无用的,因为它删除并重新创建相同的索引和外键,但我的上下文已经改变,所以我必须插入要运行的代码的迁移记录。这里生成的SQL,由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])
编辑:包含更多示例代码:
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 :(得分:2)
虽然Store模型(EF生成的sql)没有改变,但由于类更改了,因此EntityFramework看起来模型确实发生了变化,并且需要支持迁移。您可能会进行重复迁移,因为该表从模型中引用了两次。由于您知道这是您的应用程序的NOP,因此您可以安全地生成只迁移存储在数据库中的快照的空迁移。
从包管理器控制台生成空迁移:
PM> Add-Migration emptyMigration -IgnoreChanges
PM> Update-Database
请记住,通过生成空迁移,您承诺EF数据库模型将与项目中的代码匹配。