C# 嵌套注释ef核心表模型是否返回所有注释?
我有一个博客文章模型,它将评论和回复保存在同一个表中C# 嵌套注释ef核心表模型是否返回所有注释?,c#,entity-framework-core,ef-model-first,C#,Entity Framework Core,Ef Model First,我有一个博客文章模型,它将评论和回复保存在同一个表中 public class Comment : Entity, IAggregateRoot { private readonly List<Comment> _replies; public Comment() { _replies = new List<Comment>(); } public string CommentText { get; set; }
public class Comment : Entity, IAggregateRoot
{
private readonly List<Comment> _replies;
public Comment()
{
_replies = new List<Comment>();
}
public string CommentText { get; set; }
public User User { get; set; }
public Post Post { get; set; }
public IReadOnlyList<Comment> Replies => _replies;
}
但这会返回没有回复的回复
我需要从数据库中获取嵌套注释层次结构
有人能帮忙吗
谢谢实体配置中的自引用设置正确吗?“返回所有注释”-嗯,是的,所有注释都与帖子关联。不确定是否能够直接从查询中获取所需内容(至少,我放弃了尝试)。在显示注释之前预处理注释可能更容易,自己构建树也更容易(另外,编写递归方法也是一个很好的借口)。
public async Task<IEnumerable<Comment>> GetCommentsByPostId(Guid postId)
{
return await Context.Comments
.AsNoTracking()
.Include(c => c.Post)
.Include(c => c.User)
.Include(c => c.Replies)
.Where(c => c.Post.Id == postId)
.ToListAsync();
}
.Where(c => c.Post.Id == postId && c.Replies.count() == 0)