简体   繁体   English

如何在 EF Core 中查询多对多关系

[英]How to query many-to-many releationship in EF Core

I'm using .NET Core and EF Core for a web project.我正在将 .NET Core 和 EF Core 用于 Web 项目。 I'm struggling how to query a many-to-many releationship.我正在努力如何查询多对多关系。 This is what my models look like:这是我的模型的样子:

public class Begrip
{
    public int ID { get; set; }
    public string Name { get; set; } 
    public string Desc { get; set; }
    [Url]
    public string URL { get; set; } 
    public ICollection<BegripCategory> Categories { get; set; } 
}

public class Category
{
    public int ID { get; set; }
    public string Name { get; set; } 
    public ICollection<BegripCategory> Begrippen { get; set; }
}

public class BegripCategory
{
    public int begripId { get; set; }
    public Begrip begrip { get; set; } 
    public int categoryId { get; set; }
    public Category category { get; set; } 
}

EF Core won't load related properties automatically, so you'll need to explicitly do this, but something like the following should do the trick: EF Core 不会自动加载相关属性,因此您需要显式执行此操作,但类似以下内容应该可以解决问题:

var result = context.Begrip
    .Include(x => x.Categories)
    .ThenInclude(x => x.category);

Note, intellisense doesn't always work on .ThenInclude at the moment, but the code should still compile even if it gets a red underline.请注意,intellisense 目前并不总是在.ThenInclude上工作,但即使它得到红色下划线,代码仍应编译。

If you're returning this to the view or an API, you'll likely want to map it to a DTO so you don't have to deal with .Categories[0].category.Name etc.如果您将其返回到视图或 API,您可能希望将其映射到 DTO,这样您就不必处理.Categories[0].category.Name等。

If you need to filter a Many-to-Many relationship describe below I recomend to use a LinQ Enumerable Any method like this:如果您需要过滤下面描述的多对多关系,我建议使用这样的LinQ Enumerable Any 方法

return result.Where(x => x.Categories.Any(c => c.category == categoryId));

To return a filtered list of entities related by a specific category.返回与特定类别相关的实体的过滤列表。

EntityFrameworkCore Relationship query example EntityFrameworkCore 关系查询示例

Extending @Richard's answer :扩展@Richard 的答案:

I noticed in Visual Studio 2017 15.5.6 when I do following:当我执行以下操作时,我在 Visual Studio 2017 15.5.6 中注意到:

            return _context.Begrip
            .Include(x => x.Categories)
                .ThenInclude(y => y.<nothing typed in here yet>)

IntelliSense at first tells me that y if of type ICollection of BegripCategory presenting methods suitable for collections what is confusing especially that when I start typing "category" (in place of "nothing typed in here yet") IntelliSense changes as if we were dealing with only a single instance instead of ICollection IntelliSense 起初告诉我,如果是BegripCategoryICollection类型的y ,表示适用于集合的方法,特别是当我开始输入“类别”(代替“这里没有输入任何内容”)时,IntelliSense 会发生变化,就好像我们正在处理只有一个实例而不是ICollection

Just a tiny remark, but I hope it will help to save a few minutes time confusion.只是一个小小的评论,但我希望它有助于节省几分钟的时间混乱。

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

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