【发布时间】:2016-08-04 08:22:29
【问题描述】:
我希望我的种子方法首先清除/删除数据库并删除所有旧数据,但是
context.Database.ExecuteSqlCommand("TRUNCATE TABLE [Purchases]");
context.Database.ExecuteSqlCommand("TRUNCATE TABLE [Invoices]");
给我
无法截断表“Purchases”,因为它正被 FOREIGN KEY 约束。
因为采购中的条目依赖于发票中的条目。如何通过种子方法清除所有数据?
编辑:这些是相关模型:
public class Invoice
{
//Primary Key
public int InvoiceID { get; set; }
//Misc. info
public DateTime CreationDate { get; set; }
public DateTime DeadlineDate { get; set; }
public string ReceiverName { get; set; }
//Order details
public virtual List<Purchase> Purchases { get; set; }
//Auto-calculated property
[DataType(DataType.Currency)]
public float TotalCost { get; set; }
//Invoice author info
public string AuthorName { get; set; }
public string AuthorID { get; set; }
}
public class Purchase
{
public int PurchaseID { get; set; }
public string ProductDescription { get; set; }
public float SinglePrice { get; set; }
public float Amount { get; set; }
public float TotalPrice { get { return Amount * SinglePrice; } }
}
【问题讨论】: