Reza
Reza

Reputation: 5634

How to Eager Load associated data in EF Core?

I have a Gig Model as follows:

public class Gig
{
    public int Id { get; set; }

    [Required]
    public ApplicationUser Artist { get; set; }

    [Required]
    public string ArtistId { get; set; }

    public DateTime DateTime { get; set; }

    [Required]
    [StringLength(255)]
    public string Venue { get; set; }

    [Required]
    public Genre Genre { get; set; }

    [Required]
    public byte GenreId { get; set; }
}

In EF6, I was able to Eager Load Artist and Genre using the following code

            var gigs = _context.Attendances
            .Where(a => a.AttendeeId == userId)
            .Select(a => a.Gig)
            .Include(a => a.Artist)
            .Include(a => a.Genre)
            .ToList();

But with EF Core, the Artist info or the Genre info is not getting loaded. SQL Profiler shows that there is no INNER JOIN being called on the projection tables.

SELECT [a.Gig].[Id], [a.Gig].[ArtistId], [a.Gig].[DateTime], [a.Gig].[GenreId], [a.Gig].[Venue]
FROM [Attendances] AS [a]
INNER JOIN [Gigs] AS [a.Gig] ON [a].[GigId] = [a.Gig].[Id]
WHERE [a].[AttendeeId] = @__userId_0',N'@__userId_0 nvarchar(450)',@__userId_0=N'469d8515-9a04-46af-9276-09c6fead9e10'

Can someone help me re-write the query for EF Core please to include the projection tables?

eager load

UPDATE: added link to db schema scripts here. posting just the gigs table here:

CREATE TABLE [dbo].[Gigs](
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [ArtistId] [nvarchar](450) NOT NULL,
    [DateTime] [datetime2](7) NOT NULL,
    [GenreId] [tinyint] NOT NULL,
    [Venue] [nvarchar](255) NOT NULL,
 CONSTRAINT [PK_Gigs] PRIMARY KEY CLUSTERED 
(
    [Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO
ALTER TABLE [dbo].[Attendances]  WITH CHECK ADD  CONSTRAINT [FK_Attendances_AspNetUsers_AttendeeId] FOREIGN KEY([AttendeeId])
REFERENCES [dbo].[AspNetUsers] ([Id])
GO
ALTER TABLE [dbo].[Attendances] CHECK CONSTRAINT [FK_Attendances_AspNetUsers_AttendeeId]
GO
ALTER TABLE [dbo].[Attendances]  WITH CHECK ADD  CONSTRAINT [FK_Attendances_Gigs_GigId] FOREIGN KEY([GigId])
REFERENCES [dbo].[Gigs] ([Id])
GO
ALTER TABLE [dbo].[Attendances] CHECK CONSTRAINT [FK_Attendances_Gigs_GigId]
GO
ALTER TABLE [dbo].[Gigs]  WITH CHECK ADD  CONSTRAINT [FK_Gigs_AspNetUsers_ArtistId] FOREIGN KEY([ArtistId])
REFERENCES [dbo].[AspNetUsers] ([Id])
ON DELETE CASCADE
GO
ALTER TABLE [dbo].[Gigs] CHECK CONSTRAINT [FK_Gigs_AspNetUsers_ArtistId]
GO
ALTER TABLE [dbo].[Gigs]  WITH CHECK ADD  CONSTRAINT [FK_Gigs_Genres_GenreId] FOREIGN KEY([GenreId])
REFERENCES [dbo].[Genres] ([Id])
ON DELETE CASCADE
GO
ALTER TABLE [dbo].[Gigs] CHECK CONSTRAINT [FK_Gigs_Genres_GenreId]
GO

Table relationships

Upvotes: 3

Views: 1463

Answers (1)

Ivan Stoev
Ivan Stoev

Reputation: 205889

If you turn on EF Core Logging, you'll see inside the log something like this:

The Include operation for navigation: 'a.Gig.Artist' was ignored because the target navigation is not reachable in the final query results.

and similar for a.Gig.Genre.

Looks like EF Core at this time cannot handle includes for such queries (that don't start from the resulting entity). The only workaround I can propose is to rewrite the query like this:

var gigs = _context.Gigs
    .Where(g => g.Attendances.Any(a => a.AttendeeId == userId))
    .Include(g => g.Artist)
    .Include(g => g.Genre)
    .ToList();

or this (translates to better SQL, although the SQL execution plan could be the same):

var gigs = (from g in _context.Gigs
            from a in g.Attendances
            where a.AttendeeId == userId
            select g)
    .Include(g => g.Artist)
    .Include(g => g.Genre)
    .ToList();

Upvotes: 1

Related Questions