I am working on a Timesheet system. I'm using Entity Framework for the first time and am confused by how the relationships work.
I have the following class, Week:
public class Week
{
public int WeekId { get; set; }
public DateTime FirstDayOfWeek { get; set; }
public List<TimeEntry> TimeEntries { get; set; }
public Week()
{
TimeEntries = new List<TimeEntry>();
}
}
and each week contains a number of TimeEntries:
public class TimeEntry
{
public int TimeEntryID { get; set; }
public double MonHours { get; set; }
public double TueHours { get; set; }
public double WedHours { get; set; }
public double ThuHours { get; set; }
public double FriHours { get; set; }
}
The way that EF has created the tables for these is as follows:
dbo.Weeks
WeekId (int)
FirstDayOfWeek (datetime)
dbo.TimeEntries
TimeEntryId (int)
MonHours (float)
TueHours (float)
WedHours (float)
ThuHours (float)
FriHours (float)
Week_WeekId (int)
In my tables I have the following:
WeekId---FirstDayOfWeek
1------------2017-07-31
TimeEntryId---MonHours---TueHours...-Week_WeekId
1-----------------1---------------2...-------------1
2-----------------0---------------1...-------------1
(Apologies for the badly drawn tables!)
The problem is when I try and retrieve the data from my controller using Linq, it doesn't pull back the list of TimeEntries.
This is the query I'm using:
Week SelectedWeek = (from w in db.Weeks
where w.WeekId == 1
select w).FirstOrDefault();
However, the count of SelectedWeek.TimeEntries is zero?
I also tried the opposite approach (which seemed more logical from a non-EF perspective):
var timeEntries = from te in db.TimeEntries
where te.week_weekId == 1
select te;
but this gives the error:
TimeEntry does not contain a definition for week_weekId...
What is the right way to do this?