I am working with EF Core. I have a table structure like this:
public class User
{
public User()
{
this.Projects = new HashSet<Project>();
}
[Key]
public int Id { get; set; }
public string name { get; set; }
public string emailId { get; set; }
public virtual ICollection<Project> Projects { get; set;}
}
public class Project
{
public Project()
{
this.TimeSheetData = new HashSet<TimeSheetData>();
}
[Key]
public int Id { get; set; }
public string Name { get; set; }
public int userId { get; set; }
[ForeignKey("userId")]
public virtual User User {get; set; }
public virtual ICollection<TimeSheetData> TimeSheetData { get; set;}
}
public class TimeSheetData
{
[Key]
public int id { get; set; }
public int project_id { get; set; }
[ForeignKey("project_id")]
public virtual Project Project {get; set; }
public string hours_logged { get; set; }
}
------------
public List<User> GetTimeSheet(int userid)
{
var data = _context.Users.Include(u => u.Projects)
.ThenInclude(p => p.TimeSheetData)
.AsNoTracking()
.Where(a => a.Id == userid)
.ToList();
return data;
}
Which returns:
[
{
"id": 101,
"name": "Niranjan",
"emailId": "[email protected]",
"projects": [
{
"id": 1,
"name": "Niranjan",
"userId": 101
This object does not include timesheetdata. But when I debug my data returned from query shows all the data. So Do I need to change my user table to accommodate timesheet data but user table contains projects which in turn contains timesheet.
Can someone help me to figure it out?