我有两节课
public class InvoiceRow
{
public int Id { get; set; }
public int InvoiceId { get; set; }
public int ProductId { get; set; }
public virtual Product Product { get; set; }
public int Amount { get; set; }
}
public class Invoice
{
public int Id { get; set; }
private ICollection<InvoiceRow> _rows;
public virtual ICollection<InvoiceRow> Rows => _rows ?? (_rows = new List<InvoiceRow>());
}
Run Code Online (Sandbox Code Playgroud)
我在存储库类中使用 Update 方法
public void Update(Invoice record)
{
dB.Invoices.Update(record);
dB.SaveChanges();
}
Run Code Online (Sandbox Code Playgroud)
它适用于更新行集合中的值并添加新行,但它不会删除项目,如果我传递的对象行数少于数据库中的行数。最好的方法是什么?