1

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.

4
  • Well, if you turned on Lazy Loading it wouldn't load the Posts collection up front, but this just delays the full load until you access it. Can you elaborate why you don't want to just remove the property? Commented Dec 22, 2016 at 17:54
  • How to turn on lazy loading? Commented Dec 22, 2016 at 17:56
  • How do you know it's already pulling all the posts? Commented Dec 22, 2016 at 17:58
  • 1
    Should already be lazy loaded since your collection is virtual. Commented Dec 22, 2016 at 17:59

2 Answers 2

1

You can achieve this by two ways.

  1. Lazy Loading
  2. Explicit Loading

Lazy Loading - DbContext has a configuration setting that enables lazy loading DbContext.Configuration.LazyLoadingEnabled. This setting is true by default and therefore if you have not changed the default, the dynamic proxy will perform lazy loading. For using lazy loading it need to have two things implemented in your POCO classes.

  1. Your POCO classes must be public and not sealed.
  2. The navigation properties that you want to be lazy loaded must also be marked as virtual (which you have already done) so that Entity Framework can override the properties to include the lazy loading logic.

2- Explicit Loading - Explicit loading is like lazy loading in that related data is loaded separately, after the main data has been loaded. However, unlike lazy loading, it doesn’t automatically happen for you; you need to call a method to load the data.

Explicit loading is achieved using the DbContext.Entry method. The Entry method gives you access to all the information that the DbContext has about an entity. This goes beyond the values that are stored in the properties of the actual entity and includes things such as the state of the entity and the original values for each property when it was retrieved from the database.

Ex:

var blog = dbContext.Blogs.Where(r => r.BlogId == 1).FirstOrDefault();

context.Entry(blog)
.Collection(d => d.Posts)
.Load();
Sign up to request clarification or add additional context in comments.

Comments

0

You can control which children load with a projection:

var blogWithSomeChildren = dbContext.Blogs
                                    .Select(b => new
                                     {
                                         blog = b,
                                         selectedPosts = b.Posts.Where(p => p.PostId > 500) // or whatever criteria you want...
                                     })
                                    .Single(b => b.BlogId ==1);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.