var entriesResult = platformContext.GoalEntries
.Include(e => e.GoalUsers)
.Include(e => e.Milestone)
.Include(e => e.AggregateCollection)
.ThenInclude(e => e.Category)
.Include(e => e.AggregateCollection)
.ThenInclude(e => e.AggregateCollectionAggregates)
.ThenInclude(ac => ac.Select(aca => aca.Aggregate))
.Include(e => e.Metric)
.Select(e => e).ToList();
I am trying to select a nested object with ThenInclude that is an ICollection, but Entity Framework doesn't seem to like what I am trying to accomplish. I've tried writing it in numerous different ways as well... with just an include that chains down to the object I'm looking for, fewer ThenIncludes, etc. Am I doing this wrong? Is this a bug or a limitation?
The error happens on:
.ThenInclude(ac => ac.Select(aca => aca.Aggregate))
This is the exception I'm getting:
If you are seeing an exception, include the full exceptions details (message and stack trace).
The property expression 'ac => {from AggregateCollectionAggregate aca in ac select [aca].Aggregate}' is not valid. The expression should represent a property access: 't => t.MyProperty'. For more information on including related data, see http://go.microsoft.com/fwlink/?LinkID=746393.
EF Core version: 1.0.0
Operating system: Windows 7 64bit
Visual Studio version: VS 2015
You shouldn't need to use the Select syntax with EF Core (in fact it's not supported). Visual Studio IntelliSense gets confused with Include/ThenInclude (we're getting that fixed) but you should just be able to do a ThenInclude for the related property without using Select.
@rowanmiller I'm not entirely sure what you mean, because even without using the Select it still fails to work.
This doesn't throw an exception:
.Include(e => e.AggregateCollection)
.ThenInclude(e => e.AggregateCollectionAggregates)
But it's an IEnumerable join table, and the Aggregate object I'm trying to access in that join table is null.
@twilliamsgsnetx you should be able to write this:
c#
.Include(e => e.AggregateCollection)
.ThenInclude(e => e.AggregateCollectionAggregates)
.ThenInclude(ac => ac.Aggregate)
Well that's interesting. My Intellisense failed me here then? All these years working with .NET and I had no idea you could just select a property of the underlying type... even though it doesn't show up via Intellisense.
I don't know whether to feel stupid or not.
Time to go back and reevaluate all my other code then...
I don't know whether to feel stupid or not.
Definitely don't feel stupid 馃槃... it's really unintuitive that something wouldn't show up in IntelliSense but would compile. Include/ThenInclude are just pretty complicated for IntelliSense to pick the right overload etc., so we hit an issue that they weren't aware of (we've passed it on for them to fix).
@twilliamsgsnetx In case you are curious or you want to provide additional feedback on the Intellisense issue, you can find it at https://github.com/dotnet/roslyn/issues/8237.
Most helpful comment
@twilliamsgsnetx you should be able to write this:
c# .Include(e => e.AggregateCollection) .ThenInclude(e => e.AggregateCollectionAggregates) .ThenInclude(ac => ac.Aggregate)