简体   繁体   中英

EF CORE understanding relationships in a Gym setting

Ok I am kinda stuck I need a ef query that will get me the following data.

I have sessions which are created at the Gym I am using ef core here so

In the main grid I need to list the students that our their for the workout that day.

ID Session Name Start Date TeamId
1 Test 1 06/11/2021 1 ---
2 Test 2 05/11/2021 2 ---

I have table Students which has a team Id

ID FirstName Last Name TeamId
1 Matt Smith 1 ---
2 Martha Jones 2 ---

Team Includes the students and is linked back to the Student via Team Id

ID Name Session Id
1 Test Team 1
!2 Test Team 2 2

For Example what I want to be able to do is Pick the session that is on the start date of the 5 and display only the students attending that day.

My Poco Class for Session

public class Session
{
    public int Id { get; set; }
    public string? Name { get; set; }
    public int OccuranceType { get; set; }
    public int? StaffId { get; set; }
    public int? Day { get; set; }
    public int? Duration { get; set; }
    public DateTime? StartDate { get; set; }
    public DateTime? EndDate { get; set; }
    public int? Status { get; set; }
    public int? TeamId { get; set; }
    public Team? Team { get; set; }
    public bool? IsDeleted { get; set; }
    public bool? IsActive { get; set; }
    public string? CreatedBy { get; set; }
    public string? LastModifiedBy { get; set; }
    public DateTime? LastUpdatedDate { get; set; }
    public DateTime? CreatedDate { get; set; }
}

Student

public class Student
{   
    public int Id { get; set; }
    public int? Type { get; set; }
    public int? CoachId { get; set; }
    public virtual Coach Coach { get; set; }
    public int? TeamId { get; set; }
    public virtual Team Team { get; set; }
    public string? FirstName { get; set; }
    public string? Surname { get; set; }
    public DateTime? DOB { get; set; }
    public decimal? Weight { get; set; }
    public decimal? Height { get; set; }
    public int? Gender { get; set; }
    public string? Photo { get; set; }
    public int? Age { get; set; }
    public string? AddressLine1 { get; set; }
    public string? AddressLine2 { get; set; }
    public string? State { get; set; }
    public string? ZipCode { get; set; }
    public string? Mobile { get; set; }
    public string? EmailAddress { get; set; }
    public ICollection<ConditioningWorkout> ConditioningWorkouts { get; set; }
    public ConditioningWorkout? ConditioningWorkout { get; set; }
    public ICollection<Booking> Bookings { get; set; }
    public ICollection<BikeWorkOut> BikeWorkOuts { get; set; }
    public bool? IsDeleted { get; set; }
    public ICollection<Notes>? Notes { get; set; }
    public decimal? TB { get; set; }
    public decimal? OP { get; set; }
    public decimal? PU { get; set; }
    public decimal? PB { get; set; }
    public decimal? BP { get; set; }
    public int Status { get; set; }
    public bool? IsActive { get; set; }
    public string? CreatedBy { get; set; }
    public string? LastModifiedBy { get; set; }
    public DateTime? LastUpdatedDate { get; set; }
    public DateTime? CreatedDate { get; set; }
}

Team

public  class Team
{
    public int Id { get; set; }
    public Guid? UserId { get; set; }
    public Guid? TennantId { get; set; }
    public string Name { get; set; }
    public int? CoachId { get; set; }
    public virtual Coach Coach { get; set; }
    public ICollection<Student> Students {  get; set;}
    public bool? IsDeleted { get; set; }
    public int? SessionId { get; set; }
    public bool? IsActive { get; set; }
    public string? CreatedBy { get; set; }
    public string? LastModifiedBy { get; set; }
    public DateTime? LastUpdatedDate { get; set; }
    public DateTime? CreatedDate { get; set; }
}

I think I need to have some link back from the Student to the session but because they can block book sessions and team level I dont no how to achieve this.

It looks like a Session has a Team(s?) and a Team has a Student(s), so it's reasonably straight forward:

context.Sessions.Include(s => s.Team).ThenInclude(t => t.Students)
  .Where(s => s.StartDate == new DateTime(2021, 11, 5))

This gets you a collections of sessions that should have their Team property populated with a Team, and in turn that Team has a Students collection that is populated with Students

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM