更改存储库中的相关数据

Changing related data in repository

有 2 个实体 Order 和 OrderDetail,它们是这样绑定的:

public partial class Order
{ 
    public Order()
    { 
        OrderDetails = new List<OrderDetail>();
    }
    ...
}
public class OrderDetail
{
    public long OrderDetailId { get; set; }
    public int OrderId { get; set; }

    [DefaultValue(0)]
    public int RowNumber { get; set; }
    [ForeignKey("Product")]
    public int ProductId { get; set; }
    public int Count { get; set; }
    public decimal Price { get; set; }
    [ForeignKey("ProductId")]
    public virtual Product Product { get; set; }
    public virtual Order Order { get; set; }
}

我需要更改订单并从服务订单中删除一些详细信息。我在此服务中仅注入 IRepository<Order>(而非 IRepository<OrderDetail>)。
问题是:我只能通过 IRepository<Order> 来做到这一点吗(IRepository<OrderDetail>DbContext 都不明确)。

=========根据评论实现IRepository ==========

public class EFRepository<T> : IRepository<T> where T : class { 
    private readonly DBContext _context; 
    public EFRepository() { 
        _context = new DBContext(); 
    } 
    public T Add(T item) { 
        _context.Set<T>().Add(item); 
        _context.SaveChanges(); return item; 
    } 
    public T Update(T entity) { 
        _context.Entry(entity).State = EntityState.Modified;
        _context.SaveChanges(); 
        return entity; 
    }
    public void Delete(T item) { 
       if (item!=null) _context.Set<T>().Remove(item); 
       _context.SaveChanges(); 
    } 
    public T GetById(object id) { 
        return _context.Set<T>().Find(id); 
    }
    public IQueryable<T> GetAll(Expression<Func<T, bool>> predicate = null) { 
        return (predicate == null) ? _context.Set<T>() : _context.Set<T>().Where(predicate); 
    } 

解决方案是实体 OrderDetails 中的键,(删除 OrderDetailId 并将 OrderId 和 RowNumber 设置为主复合键(感谢一位男士的暗示,但他的 post 已被版主删除)

public class OrderDetail
    {
      //  public long OrderDetailId { get; set; }

         [Key, Column(Order = 1)]
        public int OrderId { get; set; }

        [Key, Column(Order = 2)]
        public int RowNumber { get; set; }

        [ForeignKey("Product")]
        public int ProductId { get; set; }
        public int Count { get; set; }
        public decimal Price { get; set; }
        [ForeignKey("ProductId")]
        public virtual Product Product { get; set; }
        public virtual Order Order { get; set; }

    }

比下面的代码更改数据和删除不必要的 OrderDetails

 public Order ChangeOrderSimple(Guid guid, IEnumerable<JsonModelItem> items, string Comments, bool isReserve, DateTime? DeliveryDate = null)
        {
            decimal total = 0;
            int row = 1;
            Dictionary<int, int> codes = items.ToDictionary(p => p.ProductId,p=>p.Count);
            var OrderToChange = _orders.GetAll(q => q.GuidIn1S == guid).Include(o => o.OrderDetails).FirstOrDefault();
            OrderToChange.Comments = Comments;
            OrderToChange.isReserve = isReserve;
            OrderToChange.DeliveryDate = DeliveryDate;


            var OrderDetails = OrderToChange.OrderDetails.ToList();

            List<OrderDetail> OrderDetailsChanged = new List<OrderDetail>();

            for (int i = 0; i < OrderDetails.Count; i++)
            {
                var item = OrderDetails[i];
                //    item = OrderD.OrderDetails[i];
                var result = codes.ContainsKey(item.ProductId);


                if (result)
                {
                    OrderDetail detail = new OrderDetail
                    {
                        RowNumber = ++row,
                        ProductId = item.ProductId,
                        Count = codes[item.ProductId],
                        Price = item.Price,
                        OrderId = item.OrderId
                    };
                    OrderDetailsChanged.Add(detail);
                }
            }

            OrderToChange.OrderDetails = OrderDetailsChanged;
            OrderToChange.CalculateTotal();

            _orders.Update(OrderToChange);
            return OrderToChange;
        }