Entity relationship in Entity Framework code-first

30 Views Asked by At

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.

2

There are 2 best solutions below

0
On

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);
0
On

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();