C# 实体框架核心未从引用表加载相关数据

C# 实体框架核心未从引用表加载相关数据,c#,asp.net-mvc,asp.net-core,C#,Asp.net Mvc,Asp.net Core,我的模型M:M关系引用到 型号 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 {

我的模型
M:M
关系引用到

型号

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

}
公共类职位
{
[关键]
公共int Id{get;set;}
[显示(Name=“创建人:”)]
公共AppUser AuthorId{get;set;}
[必需]
公共字符串标题{get;set;}
公共字符串元标题{get;set;}
[必需]
公共字符串体{get;set;}
公共bool已发布{get;set;}
公共布尔值为{get;set;}
public DateTime CretedDate{get;set;}=DateTime.Now;
public DateTime ModifiyDate{get;set;}=DateTime.Now;
公共IList注释{get;set;}
公共IList后标记{get;set;}
公共IList后分类{get;set;}
公共IList映像{get;set;}
}
公共类标签
{
[关键]
公共int Id{get;set;}
[必需]
公共字符串名称{get;set;}
public bool Published{get;set;}=true;
public DateTime CretedDate{get;set;}=DateTime.Now;
public DateTime ModifiyDate{get;set;}=DateTime.Now;
公共IList后标记{get;set;}
公共IList映像{get;set;}
}
公共类PostTag
{
public int TagId{get;set;}
公共int PostId{get;set;}
公共Post Post{get;set;}
公共标记{get;set;}
公共AppUser AppUser{get;set;}
}
DB上下文

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);
modelBuilder.Entity()
.HasMany(c=>c.Comments)
。其中一个(e=>e.Post);
modelBuilder.Entity().HasKey(p=>new
{
p、 职位,p.类别
});
modelBuilder.Entity()
.HasOne(p=>p.post)。
WithMany(p=>p.PostCategory)。
HasForeignKey(p=>p.PostId);
modelBuilder.Entity()。
HasOne(p=>p.Category)。
WithMany(p=>p.PostCategory)。
HasForeignKey(p=>p.CategoryId);
在控制器上,在获取所有POST的一侧,它会获取所有POST,但不会从相关表中获取任何数据。标签、类别示例

控制器

public async Task<IActionResult> Index()
{
    return View(await _context.Post.ToListAsync());
}
公共异步任务索引()
{
返回视图(wait_context.Post.ToListAsync());
}

更新操作

标记引用为空


试试
\u context.Post.Include(x=>x.PostCategory)
等等


参考:

使用包含继续包含更多级别的相关数据

 var posts = _context.Posts.Include(p => p.PostTag).ThenInclude(pt => pt.Tag).ToList();

请尝试
\u context.Post.Include(x=>x.PostCategory)
,因此onEfCore使用“急切加载”作为默认值,您需要使用.Include扩展名指定加载深度。我更改了它仍然没有数据显示请检查图像2这意味着
PostCategory
没有任何项目但是快速问题您能检查下一张图像吗它正在获取标签id但参考是空的