SQLite和实体框架核心:为什么包括不起作用?

时间:2017-11-05 00:29:46

标签: c# entity-framework sqlite include entity-framework-core

我使用EntityFrameworkCore.SQLite v1.1.3,VS 2015,制作简单的WPF应用程序,.Net 4.6
PeopleOrders表与#34;多对多相关"通过我的数据库中的OrdersToPeople表。我使用SQLite Tools制作了dbContext类 我使用此代码检查加载的数据:

var list = myDbContext.People
    .Include(t => t.OrdersToPeople);
foreach (var element in list)
{
    var c = element.OrdersToPeople.Count;
    //c is always 0. Why?
}

当我以相同的方式加载OrdersToPeopleOrders个表时

var list = myDbContext.OrdersToPeople

var list = myDbContext.Orders

,我看到了数据。当我进行SQL查询时,它返回正确的数据。

为什么Include无法加载OrdersToPeople

P.S。 OrdersToPeople字段为virtual

public partial class People
{
    //...fields...
    public virtual ICollection<OrdersToPeople> OrdersToPeople { get; set; }
    public People()
    {
        OrdersToPeople = new HashSet<OrdersToPeople>();
    }
}
public partial class OrdersToPeople
{
    public long Id{ get; set; }
    public long PeopleId { get; set; }
    public long OrderId { get; set; }
    public virtual People People { get; set; }
    public virtual Orders Orders { get; set; }
}

1 个答案:

答案 0 :(得分:0)

我认为这种行为是可以预期的。您是否尝试在选择中调用ToList?

看起来EF在没有ToList的情况下忽略了包含: Entity Framework Core ignoring .Include(..) without .ToList(..) indirectly

var list = myDbContext.People
    .Include(t => t.OrdersToPeople);
    .ToList();