如何在EF Core中加載關聯數據?

asp.net-core entity-framework entity-framework-core

我有一個Gig模型如下:

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; }
}

在EF6中,我能夠使用以下代碼進行Eager Load ArtistGenre

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

但是使用EF Core,藝術家信息或流派信息無法加載。 SQL事件探查器顯示在投影表上沒有調用INNER JOIN。

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'

有人可以幫我重新編寫EF Core的查詢請包含投影表嗎?

急切的負荷

更新: 在此處添加了到db模式腳本的鏈接。在這裡發布演出表:

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

表關係

一般承認的答案

如果打開EF Core Logging ,您會在日誌中看到如下內容:

導航的包含操作:'a.Gig.Artist'被忽略,因為在最終查詢結果中無法訪問目標導航。

和類似的a.Gig.Genre

看起來EF Core此時無法處理包含此類查詢(不從生成的實體開始)。我可以提出的唯一解決方法是重寫這樣的查詢:

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

或者這(轉換為更好的SQL,儘管SQL執行計劃可能是相同的):

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();


Related

許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow
許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow