小编Ole*_*rov的帖子

Entity Framework Core,从嵌套集合中删除项目

我有两节课

 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)

它适用于更新行集合中的值并添加新行,但它不会删除项目,如果我传递的对象行数少于数据库中的行数。最好的方法是什么?

c# entity-framework entity-framework-core

8
推荐指数
1
解决办法
3342
查看次数