SQLite and Entity Framework Core: why include doesn't work?

c# entity-framework entity-framework-core include sqlite

Question

I use EntityFrameworkCore.SQLite v1.1.3, VS 2015, make simple WPF application, .Net 4.6
People and Orders tables are related as "many-to-many" through OrdersToPeople table in my database. I've made dbContext classes using SQLite Tools.
I use this code to check loaded data:

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

When i load OrdersToPeople or Orders tables the same way

var list = myDbContext.OrdersToPeople

or

var list = myDbContext.Orders

, i see the data. When i make SQL-query, it returns me correct data.

Why Include does not load OrdersToPeople?

P.s. The OrdersToPeople field is 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
11/5/2017 12:29:46 AM

Popular Answer

I think the behaviour is expected. Did you try to invoke ToList on your selection?

It looks like EF is ignoring include without ToList: Entity Framework Core ignoring .Include(..) without .ToList(..) indirectly

var list = myDbContext.People
    .Include(t => t.OrdersToPeople);
    .ToList();
1
11/22/2017 2:07:42 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow