Closed bartekkois closed 1 year ago
Select
overrides Include
s
See this https://github.com/aspnet/EntityFramework/issues/6177#issuecomment-236229259
(I think it's very confusing and not intuitive, but that's how it works.)
@bartekkois
You already answered it in your issue, just remove the Select, use Include
and ThenIncldue
currently you can't traverse back the tree, so you need to start again:
var notifications = _context.UserNotifications .Where(un => un.UserId == userId) .Include(un => un.Notification) .ThenInclude(g => g.Artist) .Include(un => un.Notification) .ThenInclude(g => g.Genre) .ToList();
p.s. I'm not a MS employee, you might want to wait for an official answer, but that's AFAIK the answer.
What @gdoron suggested is the correct workaround.
@bartekkois I am going to mark this as an answered question, but:
FWIW, I don't think this query would have returned the related entities in EF6 either. In EF6 the paths passed to Include()
have to represent valid navigation paths from the result type of the query.
I have found that exmaple in https://app.pluralsight.com/library/courses/full-stack-dot-net-developer/table-of-contents. Exactly two of them:
var gigs = _context.Attendances
.Where(u => u.AttendeeId == userId)
.Select(a => a.Gig)
.Include(a => a.Artist)
.Include(g => g.Genre)
.ToList();
var notifications = _context.UserNotifications
.Where(un => un.UserId == userId && un.IsRead == false)
.Select(un => un.Notification)
.Include(g => g.Gig.Artist)
.ToList();
Moreover workround presented by @gdoron does not return Gig entities which are essential for this query.
Hi Currently I am walking through one of ASP.NET tutorials and I`m trying to run it on ASP.NET Core (with EF Core). However there is a problem with one of LINQ queries like below. It should return a collection of Notification objects with Artist and Genre, but it returns only Notifications with Artis and Genre set to null (UserNotifications is a many-to-many relationship between Notifications and User; Notifications is related to Gig ang Gig to Artist).
Problem seems to be solved when I add the following query before:
Why this happens and How can I put this in a single LINQ query in EF Core?
Further technical details
EF Core version: 1.0.0 Operating system: Windows 10 Visual Studio version: VS 2015 U3