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

Fred
  • 2,965
  • 4
  • 29
  • 51
ZCoder
  • 1,731
  • 5
  • 20
  • 41
  • try `_context.Post.Include(x => x.PostCategory)` and so on – vivek nuna Jun 21 '20 at 07:31
  • EfCore uses eager loading as a default and you need to specify the loading depth using the.Include extension. https://stackoverflow.com/a/29092178/314291 – StuartLC Jun 21 '20 at 07:32

2 Answers2

2

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

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

vivek nuna
  • 12,695
  • 7
  • 48
  • 123
2

Use ThenInclude to continue including further levels of related data.

 var posts = _context.Posts.Include(p => p.PostTag).ThenInclude(pt => pt.Tag).ToList();
mj1313
  • 6,367
  • 1
  • 2
  • 22