Include after Union method not working (Entity Framework Core 2)

.net c# entity-framework entity-framework-core linq-to-entities

Question

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.

1
1
11/27/2017 12:56:17 PM

Popular Answer

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; 
 }
2
12/4/2017 3:38:14 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