简体   繁体   English

在 Entity Framework .NET5 中获取多级相关记录

[英]Fetching multiple levels of related records in Entity Framework .NET5

I have the following code.我有以下代码。 It brings back the Show and its related Slides .它带回了Show及其相关的Slides However Slide also has related Items but I have no idea how to make the query include those (There is a foreign key in the db).然而幻灯片也有相关的项目,但我不知道如何让查询包括那些(数据库中有一个外键)。

Show sh = (from s in _context.Shows
           where s.ShowId == id
           select new Show()
           {
               ShowId=s.ShowId,
               ShowName=s.ShowName,
               Slides=s.Slides           
           }).FirstOrDefault();

How do I modify this to make it also fetch the list of Items for each Slide ?如何修改它以使其也获取每个SlideItems列表?

I am using.Net5我正在使用.Net5

If you defined your DbContext correctly the LINQ would be如果您正确定义了 DbContext,则 LINQ 将是

Show sh = _context.Shows
    .Where(s => s.ShowId == id)
    .Include(s => s.Slides)
    .ThenInclude(sl => sl.Items)
    .FirstOrDefault();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM