EF Core新手......
我有以下EF Core类设置,但是我遇到了将数据加载到视图中的问题。当为每个表填充所有数据时,所有数据都按原样加载,但是,例如,如果我有订单和订单行没有订单只接收订单加载。订单行不会加载。
Classes:
public class Order {
public Guid OrderID { get; set;}
public ICollection<OrderLine> OrderLines { get; set; }
}
public class OrderLine {
public Guid OrderLineId { get; set; }
public int OrderQty { get; set; }
public Order Order { get; set; }
public Guid OrderID { get; set; }
public ICollection<OrderReceived> OrdersReceived { get; set; }
}
public class OrderReceived {
public Guid OrderReceivedID { get; set; }
public int QuantityReceived { get; set; }
public OrderLine OrderLine { get; set; }
public Guid OrderLineId { get; set; }
}
My controller looks like this:
public async Task<IActionResult> Details(Guid? id) {
if (id == null) {
return NotFound();
}
var orderDetail = await _context.Orders
.Include(ol => ol.OrderLines)
.ThenInclude(rec => rec.OrdersReceived)
.SingleOrDefaultAsync(o => o.OrderId = id);
}
例如:订单A有1个订单行和2个OrdersReceived。这完全加载。
Ex:订单B有1个订单行,没有收到订单。只有Order详细信息会加载下面未加载的所有内容(orderline或ordersreceived)。我想我正在为收到的订单寻找更像左连接的东西。我对EF Core并不熟悉。
希望我能正确地解释这一点。任何帮助是极大的赞赏。我确实发现这篇帖子有类似的问题:ef core linq filtered child entities。最好的做法是为这种情况实现一个viewmodel吗?
谢谢你, ë