我正在创建一个 ASP.NET Core API 应用程序,并依赖于 EF Core:
public class AppUser : IdentityUser
{
public string FirstName { get; set; }
public string LastName { get; set; }
[InverseProperty(nameof(Post.Author))]
public ICollection<Post> Posts { get; set; } = new List<Post>();
}
public class Post
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public string AuthorId { get; set; }
[ForeignKey("AuthorId")]
public virtual AppUser Author { get; set; }
[InverseProperty(nameof(Like.Post))]
public ICollection<Like> Likes { get; set; } = new List<Like>();
[InverseProperty(nameof(Comment.Post))]
public ICollection<Comment> Comments { get; set; } = new List<Comment>();
}
其中 Comment
和 Like
是其他一些实体。请注意,为了简洁起见,我已经简化了实体。然后,我想得到一个用户的 Posts
,但也包括 Likes
和 Comments
,一个帖子已经得到。所以,我做了这样的事情:
return _context.Users
.Include(u => u.Location)
.Include(u => u.Posts)
.ThenInclude(p => p.Comments)
.ThenInclude(c => c.Owner)
.Include(u => u.Posts)
.ThenInclude(p => p.Likes)
.ThenInclude(l => l.Giver)
.Where(u => u.Id == userId)
.FirstOrDefault();
现在,这个工作正常,但你可以看到,我正在呼叫 .Include(u = u.Posts)
两次。有没有一种方法可以在同一属性上调用 ThenInclude
两次,而不必实际写入 Include
语句也两次?