包括不在ef-core2.0上工作

时间:2017-09-06 15:46:48

标签: linq entity-framework-core

在VS2017中处理aspnetcore2.0项目

'.Include(o => o.OrderEventLogs)'子句在以下查询中不起作用:

       var orders = _repository.GetDbSet<Order>()
             .Where(o => o.RiskCellId == int.Parse(riskCellId) && 
                    o.OrderDate.ToShortDateString() == OrderDate.ToShortDateString())
             .Include(o => o.OrderEventLogs)
             .Include(o => o.Instrument) 
             .ToList();

未加载相关的OrderEventLog实体。 (实际上有时候它确实有效,有时却没有。相同的查询,相同的参数)

这真的很奇怪,因为我运行此查询时会包含此相关实体:

        var order = _repository.GetDbSet<Order>()
            .Where(o => o.Id == Guid.Parse(orderId))
            .Include(o => o.OrderEventLogs)
            .Include(o => o.Instrument)
            .SingleOrDefault();

这两段代码在之前的ef-core版本中运行得很好

的软件包:

<PackageReference Include="Microsoft.EntityFrameworkCore" Version="2.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.0" />

更新

如果我这样做似乎工作正常:

       var orders = _repository.GetDbSet<Order>()
             .Where(o => o.RiskCellId == int.Parse(riskCellId) && 
                    o.OrderDate.ToShortDateString() == OrderDate.ToShortDateString())
             .Include(o => o.Instrument) 
             .ToList();

        foreach (Order order in orders)
        {
            _repository.Context.Entry(order).Collection(e => e.OrderEventLogs)
                .Load();
        }

0 个答案:

没有答案