Chaining multiple include with thenInclude throws System.InvalidOperationException

entity-framework-core

Question

Consider this simplified model:

public class Field 
{
   public string Name { get; set; }
   public MultilingualString Label { get; set; }
   public MultilingualString Placeholder { get; set; }
}

public class MultilingualString 
{
   public string DefaultText { get; set; }
   public IList<Globalization> Globalizations { get; set; }
}

public class Globalization 
{
   public string Text { get; set; }
   public Language Language { get; set; }
}

public class Language
{
   public string Name { get; set; }
   public string CultureString { get; set; }
}

Using my context, I am trying to get all my fields and include their Label and Placeholder along with the globalizations and language (the whole thing) by using the following command:

var field = context.Fields
   .Include(x => x.Label.Globalizations)
   .ThenInclude(x => x.Language)
   .Include(x => x.Placeholder.Globalizations)
   .ThenInclude(x => x.Language)
   .ToList();

I get the following exception thrown:

System.InvalidOperationException Invalid attempt to read when no data is present.

However, using only the first include/theninclude, I do get the expected results.

What gives? Is this a known bug, or did I misunderstand the Include/ThenInclude syntax?

This is using ef7 beta4 with VS2015 RC

Accepted Answer

This is a bug in EF 7. Should be fixed with aspnet/EntityFramework#2474, which will not be in the public EF build until beta 6. Of course, you can also try nightly builds



Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why