多对多关系仅包含一个实体

时间:2019-04-04 11:41:47

标签: .net-core entity-framework-core

Dotnet Core 2.2,EntityFrameworkCore 2.2.3

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

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

对于关系本身,我使用EF“自动魔术”进行了尝试,并在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中的DbSet

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

从控制器拨打电话

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

如果我忽略了自动完成功能,那就可以了,谢谢!

1 个答案:

答案 0 :(得分: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