如何在 EF Core 中查询多对多关系 [英] How to query many-to-many releationship in EF Core

查看:124
本文介绍了如何在 EF Core 中查询多对多关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将 .NET Core 和 EF Core 用于 Web 项目.我正在努力如何查询多对多关系.这是我的模型的样子:

公共类 Begrip{公共 int ID { 获取;放;}公共字符串名称 { 获取;放;}公共字符串 Desc { 获取;放;}[网址]公共字符串 URL { 获取;放;}公共 ICollection类别 { 得到;放;}}公开课类别{公共 int ID { 获取;放;}公共字符串名称 { 获取;放;}公共 ICollectionBegrippen { 得到;放;}}公共类 BegripCategory{公共 int begripId { 获取;放;}公共 Begrip begrip { get;放;}公共 int 类别 ID { 获取;放;}公共类别类别{获取;放;}}

和我的数据库上下文:

公共类 PBBContext : DbContext{公共 PBBContext(DbContextOptions 选项):基础(选项){}public DbSet电影{得到;放;}public DbSetBegrip { 得到;放;}public DbSet类别{得到;放;}public DbSetBegripCategory { 得到;放;}protected override void OnModelCreating(ModelBuilder modelbuilder){modelbuilder.Entity().HasKey(bc => new { bc.begripId, bc.categoryId });modelbuilder.Entity().HasOne(b => b.begrip).WithMany(bg => bg.Categories).HasForeignKey(bc => bc.begripId);modelbuilder.Entity().HasOne(c => c.category).WithMany(ca => ca.Begrippen).HasForeignKey(cc => cc.categoryId);}}

我想要做的是在 JSON 结果中返回所有Begrippen"以及所有相应的类别",但是,我不知道如何为它们获取类别"列表.

有什么想法吗?提前致谢.

解决方案

EF Core 不会自动加载相关属性,因此您需要显式执行此操作,但以下内容应该可以解决问题:

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

注意,intellisense 目前并不总是在 .ThenInclude 上工作,但即使代码有红色下划线,它仍然应该编译.

如果您要将其返回到视图或 API,您可能希望将其映射到 DTO,这样您就不必处理 .Categories[0].category.Name

I'm using .NET Core and EF Core for a web project. 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; } 
}

And my Database context:

public class PBBContext : DbContext
{
    public PBBContext (DbContextOptions<PBBContext> options)
        : base(options)
    {
    }

    public DbSet<PBB.Models.Movie> Movie { get; set; }
    public DbSet<PBB.Models.Begrip> Begrip { get; set; } 
    public DbSet<PBB.Models.Category> Category { get; set; } 
    public DbSet<PBB.Models.BegripCategory> BegripCategory { get; set; }

    protected override void OnModelCreating(ModelBuilder modelbuilder)
    {
        modelbuilder.Entity<BegripCategory>().HasKey(bc => new { bc.begripId, bc.categoryId });

        modelbuilder.Entity<BegripCategory>().HasOne(b => b.begrip).WithMany(bg => bg.Categories).HasForeignKey(bc => bc.begripId);
        modelbuilder.Entity<BegripCategory>().HasOne(c => c.category).WithMany(ca => ca.Begrippen).HasForeignKey(cc => cc.categoryId);
    }
}

What im trying to do is to return all the "Begrippen" in a JSON result with all the corresponding "Categories", however, I can't figure out how to get the list of "Categories" for them.

Any ideas? Thanks in advance.

解决方案

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:

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.

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.

这篇关于如何在 EF Core 中查询多对多关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆