1

Dotnet Core 2.2, EntityFrameworkCore 2.2.3

In a Many-to-Many relation between the entities "Post" and "Category" the linked Entity "PostCategory" returns the "Post" object but for the "Category" object only the Id and not the object itself.

Migrations and database update works fine and all three tables are created.

For the relation itself I tried it with EF "auto magic" and explicit definition of the relation in OnModelCreating in the ApplicationDbContext.

Models

Post-Model

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

Category-Model

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 Model

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

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

DbSets in ApplicationDbContext

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

Get all Posts from Service

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

    return posts;
}

Calling service from Controller

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

    ...
}

The result is seen in the screenshot.

In ApplicationDbContext I tried two versions:

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

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

Both version migrate and update with no errors and the same result.

I'm grateful for any help.

Best regards

enter image description here

Edit:

I tried the "ThenInclude" before but obviously my Visual Studio auto completion has a problem:

enter image description here

If I ignore the auto completion, then it works, thank you!

1
  • You are missing .ThenInclude(x => x.Category)
    – Ivan Stoev
    Apr 4, 2019 at 11:53

1 Answer 1

2

To eager load related data in multiple level, you have to use .ThenInclude as follows:

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

    return posts;
}

Here is the more details: Loading Related Data: Including multiple levels

5
  • you are (1) answering duplicate (2) without any explanation (3) with redundant code .Include(x => x.PostCategories).ThenInclude(pc => pc.Post)
    – Ivan Stoev
    Apr 4, 2019 at 12:05
  • @IvanStoev 1) I had started answering before your marked it as duplicate 2) Added explanation 3) Removed the redundant code. 4) Thank you :) Apr 4, 2019 at 12:13
  • Seems to be an auto completion issue, see original post.
    – Igotcha
    Apr 4, 2019 at 12:20
  • 1
    @TanvirArjel (1) you are expected to search for duplicates before answering (2) good (3) fine (4) you are welcome :)
    – Ivan Stoev
    Apr 4, 2019 at 13:22
  • I tried the "ThenInclude" but if I ignore and write the other entity then it works exactly fine. Thanks
    – Iswar
    Nov 22, 2019 at 8:52

Not the answer you're looking for? Browse other questions tagged or ask your own question.