实体框架核心多对多导航问题 [英] Entity Framework Core many-to-many navigation issues

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

问题描述

如GitHub问题所跟踪,Entity Framework Core尚未实现多对多关系.#1368 ;但是,当我按照该问题中的导航示例或

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 关系,但通常是由于缺少 lazy在EF Core中加载支持.因此,为了填充 Tag 属性,必须急于(或显式)加载它.加载相关数据部分.如果您查看包括多个级别部分,则会看到以下说明

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

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

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