Why I get not all data from base by link
https://localhost:XXXXX/api/comments
(GET request)
After update page data no longer appears ..
Responce:
[{"id":1,"text":"Comment1","userId":1,"parentCommentId":null,"user":null,"parentComment":null,"childrenComments":null},{"id":2,"text":"Comment2","userId":1,"parentCommentId":1,"user":null,"parentComment":{"id":1,"text":"Comment1","userId":1,"parentCommentId":null,"user":null,"parentComment":null,"childrenComments":[
Does not load subordinate item ..
What am I doing wrong?
// GET: api/Comments
[HttpGet]
public IEnumerable<Comment> GetComments()
{
return _context.Comments;
}
You must load the relationships as well. The two primary ways of doing that are eager-loading via Include
or lazy-loading. However, lazy-loading should be avoided in general, and especially so in cases like this. When you're serializing an object, you could end up issuing hundreds or even thousands of queries inadvertently with lazy-loading.
Long and short, add Include
clauses for the relationships you care about:
return _context.Comments
.Include(x => x.User)
.Include(x => x.parentComment)
.Include(x => x.childrenComments);
If you want more flexibility, you can employ either OData or GraphQL. Either will allow the client to selectively include the relationships they want/need, meaning you won't necessarily need to join everything every time.