Entity Framework Core 多对多导航问题 [英] Entity Framework Core many-to-many navigation issues

查看:26
本文介绍了Entity Framework Core 多对多导航问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Entity Framework Core 尚未实现多对多关系,如 GitHub 问题 第1368章;但是,当我按照该问题中的导航示例或 类似的答案 在 Stack Overflow,我的枚举未能产生结果.

Entity Framework Core has yet to implement many-to-many relationships, as tracked in GitHub issue #1368; however, when I follow the navigation examples in that issue or similar answers here at Stack Overflow, my enumeration fails to yield results.

我在照片和标签之间有一个多对多的关系.

I have a many-to-many relationship between Photos and Tags.

实施连接表后,示例显示我应该能够:

After implementing the join table, examples show I should be able to:

var tags = photo.PhotoTags.Select(p => p.Tag);

虽然没有结果,但我可以通过以下方式加载:

While that yields no results, I am able to to load via:

var tags = _context.Photos
    .Where(p => p.Id == 1)
    .SelectMany(p => p.PhotoTags)
    .Select(j => j.Tag)
    .ToList();

相关代码:

public class Photo
{
    public int Id { get; set; }
    public virtual ICollection<PhotoTag> PhotoTags { get; set; }
}

public class Tag
{
    public int Id { get; set; }
    public virtual ICollection<PhotoTag> PhotoTags { get; set; }
}

public class PhotoTag
{
    public int PhotoId { get; set; }
    public Photo Photo { get; set; }

    public int TagId { get; set; }
    public Tag Tag { get; set; }
}

protected override void OnModelCreating(ModelBuilder builder)
{
    base.OnModelCreating(builder);

    builder.Entity<PhotoTag>().HasKey(x => new { x.PhotoId, x.TagId });
}

我在其他示例中遗漏了什么?

What am I missing from other examples?

推荐答案

其实这不是专门针对many-to-many 的关系,而是总体来说缺乏懒惰在 EF Core 中加载支持.因此,为了填充 Tag 属性,必须预先(或显式)加载它.所有这些都在加载相关数据 EF Core 文档的部分.如果您查看包括多个级别部分,您会看到以下说明

In fact this is not a specific for many-to-many relationship, but in general to the lack of lazy loading support in EF Core. So in order to have Tag property populated, it has to be eager (or explicitly) loaded. All this is (sort of) explained in the Loading Related Data section of the EF Core documentation. If you take a look at Including multiple levels section, you'll see the following explanation

您可以使用 ThenInclude 方法通过关系向下钻取以包含多个级别的相关数据.以下示例加载所有博客、其相关帖子以及每篇帖子的作者.

You can drill down thru relationships to include multiple levels of related data using the ThenInclude method. The following example loads all blogs, their related posts, and the author of each post.

以及加载 Post.Author 的示例,它与您的几乎相同:

and example for loading the Post.Author which is pretty much the same as yours:

using (var context = new BloggingContext())
{
    var blogs = context.Blogs
        .Include(blog => blog.Posts)
            .ThenInclude(post => post.Author)
        .ToList();
}

所以为了让这个工作

var tags = photo.PhotoTags.Select(p => p.Tag);

photo 变量应该使用这样的方法检索:

the photo variable should have been be retrieved using something like this:

var photo = _context.Photos
   .Include(e => e.PhotoTags)
       .ThenInclude(e => e.Tag)
   .FirstOrDefault(e => e.Id == 1);

这篇关于Entity Framework Core 多对多导航问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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