I have this simple code, using EF Core 2.
I perform 2 subqueries and the result is the Union of them. The problem is that the Include
method in after the Union
doesn't load the navigation properties (Role
is always null
).
public static void Main()
{
using (var context = new MyDbContext())
{
IQueryable<User> a = SomeQuery(context);
IQueryable<User> b = SomeOtherQuery(context);
var union = a.Union(b);
var resultingList = union.Include(x => x.Role); // Include doesn't work
}
}
What should I do to effectively load navigation properties after composing with the Union method?
NOTE: The problem also happens with the Concat
method.
You are accessing the IQueryable out of the context of the MyDbContext object. There is no db connection at that time. Make sure you do all db access withing the context of the MyDbContext (using scope):
public IEnumarable<User> GetItems()
{
IEnumarable<User> resultingList;
using (var context = new MyDbContext())
{
IQueryable<User> a = SomeQuery(context);
IQueryable<User> b = SomeOtherQuery(context);
var union = a.Union(b);
resultingList = union.Include(x => x.Role).ToList();
}
return resultingList;
}