Let's say I have 2 entity models -
public class Blog
{
[Key]
public int BlogId { get; set; }
public string BlogName { get; set; }
public virtual ICollection<Post> Posts { get; set; }
}
public class Post
{
[Key]
public int PostId { get; set; }
public string PostName { get; set; }
public int BlogId { get; set; }
[ForeignKey("BlogId")]
public virtual Blog Blog { get; set; }
}
Let's get a blog-
var blog = dbContext.Blogs.Where(r => r.BlogId == 1).FirstOrDefault();
The code retrieves the blog and all the posts as well. But if there is thousands of posts under this blog? It would be a performance issue.
Is there any trick not to load posts by default? I don't want to remove Posts
property from Blog
.
Thanks.
You can control which children load with a projection: