I am using Entity Framework 4.1 (Code First) as O/R mapping framework:
I have three Entities: Post, PostForUser and User, whereas a PostForUser is associated with exactly one Post and one User.
Now I want to fetch all Posts for a given user and additionally load the user which created the post:
var posts = _dbContext.PostsForUser
.Include("Post.ByUser")
.Where(x => x.WasRead == false && x.User.Id == CurrentUser.Id)
.Select(x => x.Post)
.ToArray();
Using the query above, the “ByUser” property is never loaded. I am not using lazy loading at all, but I still can’t get the loading working properly.
There aren’t any custom mapping configs or special conventions registered. Just the following entity definitions:
public class Post
{
public int Id { get; set; }
public string Content { get; set; }
public DateTime Date { get; set; }
public User ByUser { get; set; }
}
public class PostForUser
{
public int Id { get; set; }
public User User { get; set; }
public Post Post { get; set; }
public bool WasRead { get; set; }
}
Your include
.Include("Post.ByUser")applies toPostForUserbut your are selectingPostso the the include should go to the and of the select: