3

MY Model M:M relationship Reference to https://www.entityframeworktutorial.net/efcore/configure-many-to-many-relationship-in-ef-core.aspx

Models

public class Post
{
    [Key]
    public int Id { get; set; }
    [Display(Name = "Created By:")]
    public AppUser AuthorId { get; set; }
    [Required]
    public string Title { get; set; }
    public string metaTitle { get; set; }
    [Required]
    public string Body { get; set; }
    public bool Published { get; set; } 
    public bool ISFeatured { get; set; }
    public DateTime CretedDate { get; set; } = DateTime.Now;
    public DateTime ModifiyDate { get; set; } = DateTime.Now;
    public IList<Comment> Comments { get; set; }
    public IList<PostTag> PostTag { get; set; }
    public IList<PostCategory> PostCategory { get; set; }
    public IList<Images> Images { get; set; }

}

public class Tag
{
    [Key]
    public int Id { get; set; }
    [Required]
    public string Name { get; set; }
    public bool Published { get; set; } = true;
    public DateTime CretedDate { get; set; } = DateTime.Now;
    public DateTime ModifiyDate { get; set; } = DateTime.Now;
    public IList<PostTag> PostTag { get; set; }
    public IList<Images> Images { get; set; }


}

public class PostTag
{

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

    public Post Post { get; set; }

    public Tag Tag { get; set; }
    public AppUser AppUser { get; set; }

}

DB context

modelBuilder.Entity<Post>()
        .HasMany(c => c.Comments)
        .WithOne(e => e.Post);

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

modelBuilder.Entity<PostCategory>()
    .HasOne(p => p.post).
    WithMany(p => p.PostCategory).
    HasForeignKey(p => p.PostId);

modelBuilder.Entity<PostCategory>().
    HasOne(p => p.Category).
    WithMany(p => p.PostCategory).
    HasForeignKey(p => p.CategoryId);

On the controller, side fetching all posts, it is bringing all the posts but not getting any data from the related tables. Example Tags, Categories

Controller

public async Task<IActionResult> Index()
{
    return View(await _context.Post.ToListAsync());
}

enter image description here

Update Action

enter image description here

Tags reference is empty

enter image description here

2
  • try _context.Post.Include(x => x.PostCategory) and so on Commented Jun 21, 2020 at 7:31
  • EfCore uses eager loading as a default and you need to specify the loading depth using the.Include extension. stackoverflow.com/a/29092178/314291 Commented Jun 21, 2020 at 7:32

2 Answers 2

2

Use ThenInclude to continue including further levels of related data.

 var posts = _context.Posts.Include(p => p.PostTag).ThenInclude(pt => pt.Tag).ToList();
Sign up to request clarification or add additional context in comments.

Comments

2

try _context.Post.Include(x => x.PostCategory) and so on.

Reference: https://learn.microsoft.com/en-us/ef/core/querying/related-data

3 Comments

I changed it still no data showing please check image 2
It means PostCategory does not have any item
but quick question can you check the next image it fetching tags id bu the reference is empty

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.