多对多关系仅包含一个实体[重复]

问题描述 投票:0回答:1

这个问题在这里已有答案:

Dotnet Core 2.2,EntityFrameworkCore 2.2.3

在实体“Post”和“Category”之间的多对多关系中,链接的实体“PostCategory”返回“Post”对象,但对于“Category”对象仅返回Id而不是对象本身。

迁移和数据库更新工作正常,并创建了所有三个表。

对于关系本身,我尝试使用EF“auto magic”并在ApplicationDbContext中OnModelCreating中显式定义关系。

楷模

后期型号

public class Post
{
    public int Id { get; set; }
    public string Slug { get; set; }
    public string Title { get; set; }
    public string Abstract { get; set; }
    public string Content { get; set; }
    public string Author { get; set; }
    public DateTime Created { get; set; }

    public ICollection<PostCategory> PostCategories { get; set; }
}

类别,型号

public class Category
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }

    public ICollection<PostCategory> PostCategories { get; set; }
}

PostCategory模型

public class PostCategory
{
    public int PostId { get; set; }
    public Post Post { get; set; }

    public int CategoryId { get; set; }
    public Category Category { get; set; }
}

ApplicationDbContext中的DbSets

public DbSet<Post> BlogPosts { get; set; }
public DbSet<Category> BlogCategories { get; set; }
public DbSet<PostCategory> PostCategories { get; set; }

从服务获取所有帖子

public IEnumerable<Post> GetAll()
{
    var posts = _context.BlogPosts
        .Include(x => x.PostCategories);

    return posts;
}

从Controller调用服务

public IActionResult Index()
{
    var blogPosts2 = _blogService.GetAll();

    ...
}

结果显示在屏幕截图中。

在ApplicationDbContext中我尝试了两个版本:

版本1:

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

    builder.Entity<PostCategory>()
   .HasKey(x => new { x.PostId, x.CategoryId });

}

    public DbSet<Post> BlogPosts { get; set; }
    public DbSet<Category> BlogCategories { get; set; }
    public DbSet<PostCategory> PostCategories { get; set; }

版本2:

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

    builder.Entity<PostCategory>()
   .HasKey(x => new { x.PostId, x.CategoryId });

    builder.Entity<PostCategory>()
        .HasOne(pt => pt.Post)
        .WithMany(p => p.PostCategories)
        .HasForeignKey(pt => pt.PostId);

    builder.Entity<PostCategory>()
        .HasOne(pt => pt.Category)
        .WithMany(t => t.PostCategories)
        .HasForeignKey(pt => pt.CategoryId); ;
}

    public DbSet<Post> BlogPosts { get; set; }
    public DbSet<Category> BlogCategories { get; set; }
    public DbSet<PostCategory> PostCategories { get; set; }

版本都迁移和更新,没有错误和相同的结果。

我很感激任何帮助。

最好的祝福

enter image description here

编辑:

我之前尝试过“ThenInclude”但显然我的Visual Studio自动完成有一个问题:

enter image description here

如果我忽略了自动完成,那么它有效,谢谢!

.net-core entity-framework-core
1个回答
0
投票

要在多个级别急切加载相关数据,您必须使用.ThenInclude,如下所示:

public IEnumerable<Post> GetAll()
{
    var posts = _context.BlogPosts
        .Include(x => x.PostCategories)
           .ThenInclude(pc => pc.Category); 

    return posts;
}

以下是更多细节:Loading Related Data: Including multiple levels

© www.soinside.com 2019 - 2024. All rights reserved.