Load child entities in Entity Framework

I have got these POCO classes:

public class  Task
    {
        public int TaskId { get; set; }
        public int ProjectId { get; set; }
        public string Name { get; set; }
        public string Description { get; set; }
        public DateTime StartDate { get; set; }
        public DateTime? DueDate { get; set; }
        public int UserId { get; set; }
        public string Tags { get; set; }
        public string FileName { get; set; }
        public virtual Project Project { get; set; }
        public virtual List<TaskAssigned> TaskAssigns { get; set; }
        public virtual List<TaskComment> TaskComments { get; set; }
        public virtual User User { get; set; }
    }

 public class User
    {
        public int UserId { get; set; }
        public string Name { get; set; }
        public int AccountId { get; set; }
        public string Email { get; set; }
        public string Password { get; set; }
        public bool IsAdmin { get; set; }
        public virtual Account Account { get; set; }
    }

public class TaskComment
    {
        public int TaskCommentId { get; set; }
        public int TaskId { get; set; }
        public int UserId { get; set; }
        public string Comment { get; set; }
        public string FileName { get; set; }
        public virtual User User { get; set; }
    }

    public class TaskAssigned
    {
        public int TaskAssignedId { get; set; }
        public int TaskId { get; set; }
        public int UserId { get; set; }
        public virtual User User { get; set; }
    }

I have tried to get task by Id and load all related entities for this task but User entity for TaskComment and TaskAssigned is null for other users than User in Task entity:

public Task GetTaskById(int taskId)
    {
        return context.Tasks.Include("TaskAssigns").Include("TaskComments").Include("User").Where(t => t.TaskId == taskId).FirstOrDefault();
    }

Answers


The "path" in the Include is all-inclusive this means you can specify multiple properties separated with a . (dot) and EF will include all the part of the "property path".

In you case this should work:

public Task GetTaskById(int taskId)
{
    return context.Tasks
               .Include("TaskAssigns.User")
               .Include("TaskComments.User")
               .Include("User").Where(t => t.TaskId == taskId).FirstOrDefault();
}

Need Your Help

How can I toggle the background color of the body of my HTML document, with one button?

javascript jquery html css

Sorry, I'm a noob :/. I'm trying to use the toggle function, but I'm open to any other ideas on how to change the background color back and forth with a button as well.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.