实体框架核心包含无法正常工作

时间:2018-10-03 15:38:40

标签: c# .net ef-core-2.0

在EF Core下面的查询中,即使存在于所有记录中,也不会设置所包含的Shipment属性。如何将包含的属性始终包含在结果中?

List<Order> result = _context
    .Orders
    .Include(x => x.Shipment)
    .Where(x => x.Date > DateTime.UtcNow.AddDays(-7))
    .ToList();
// Count below is greater than 0
// Foreign key exists on Order and Shipment
var ordersWithNoShippingInfo = result.Where(x => x.Shipment == null)

1 个答案:

答案 0 :(得分:0)

EF核心可能会通过Included属性中的数据达到大小限制。如果返回的数据没有更新,请使用.AsNoTracking()来提高查询性能,以帮助“包含”属性设置更大的查询结果。

List<Order> result = _context
    .Orders
    .Include(x => x.Shipment)
    .Where(x => x.Date > DateTime.UtcNow.AddDays(-7))
    .AsNoTracking()
    .ToList();