Iam working on an Asp,net core 5
project targeted .net 5
with Entity framework core
.
I have these Entities:
Student:
[ Key ] public string Id { get; set; }
[ Required ] public string FirstName { get; set; }
[ Required ] public string FamilyName { get; set; }
[ Required ] public DateTime BirthDate { get; set; }
[ Required ] public string BirthPlace { get; set; }
[ Required ] public Gender Gender { get; set; }
[ Required ] public Nationality Nationality { get; set; }
[ Required ] public string Address { get; set; }
[ Required ] public DateTime DateOfRegistration { get; set; }
[ Required ] public int StudyLevelId { get; set; }
[ Required ] [ForeignKey( nameof(AcademicYear))] public string AcademicYearOfRegistration { get; set; }
[ Required ] public string GroupId { get; set; }
[ Phone ] public string PhoneNumber { get; set; }
[ EmailAddress ] public string Email { get; set; }
public string FullName => $"{FirstName} {FamilyName}";
public string NationalId { get; set; }
public string PassportId { get; set; }
public string ResideneCardId { get; set; }
public string StudentGuardianId { get; set; }
public byte[] Picture { get; set; }
public virtual StudentGuadian StudentGuadian { get; set; }
public virtual StudyLevel StudyLevel { get; set; }
public virtual AcademicYear AcademicYear { get; set; }
public virtual Group Group { get; set; }
public virtual ICollection<StudentAbsence> Absences { get; set; }
public virtual ICollection<TestMark> TestMarks { get; set; }
public virtual ICollection<ExamMark> ExamMarks { get; set; }
public virtual ICollection<StudentPayment> Payements { get; set; }
public virtual ICollection<StudentMonthlyPayement> MonthlyPayements { get; set; }
public virtual StudentTrainingMark TrainingMark { get; set; }
public virtual StudentProjectMark ProjectMark { get; set; }
public virtual ICollection<StudentYearlyResult> YearlyResults { get; set; }
public virtual StudentDiplomaResult DiplomaResult { get; set; }
StudentAbsence:
[ Key ] [ DatabaseGenerated( DatabaseGeneratedOption.Identity ) ] public int Id { get; set; }
[ Required ] public DateTime AbsenceDate { get; set; }
[ Required ] public TimeSpan AbsenceTime { get; set; }
[ Required ] public bool IsJustified { get; set; }
[ Required ] public string StudentId { get; set; }
[ Required ] public string SchoolSubjectId { get; set; }
public byte[] DocOfJustify { get; set; }
public string Note { get; set; }
public string Reason { get; set; }
public virtual Student Student { get; set; }
public virtual SchoolSubject SchoolSubject { get; set; }
public string CreatedBy { get; set; }
public DateTime CreatedOn { get; set; }
public bool IsEdited { get; set; }
public string LastEditor { get; set; }
public DateTime LastEditDate { get; set; }
SchoolSubject:
[Key]
public string Id { get; set; }
[Required]
public string FullTitle { get; set; }
[Required]
public string Coefficient { get; set; }
[Required]
public byte NumberOfTests { get; set; }
[Required]
public SchoolSubjectStatus Status { get; set; }
[Required]
public string BrancheId { get; set; }
[Required]
public string SemesterId { get; set; }
[Required]
public byte FormationYearId { get; set; }
public virtual Branche Branche { get; set; }
public virtual Semester Semester { get; set; }
public virtual FormationYear FormationYear { get; set; }
public virtual ICollection<PedagogicalSequence> PedagogicalSequences { get; set; }
public virtual ICollection<StudentAbsence> StudentAbsences { get; set; }
public virtual ICollection<Test> Tests { get; set; }
public virtual ICollection<Exam> Exams { get; set; }
public virtual ICollection<GroupProfSubject> Professors { get; set; }
public virtual ICollection<ProfAbsence> ProfAbsences { get; set; }
FormationYear:
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public byte Id { get; set; }
[Required]
public string Title { get; set; }
public virtual ICollection<SchoolSubject> SchoolSubjects { get; set; }
What is the problem?
In a method I get a student
object, and I load all his Absences
, also I tried to load Absence.SchoolSubject
property (From Absence
) and Absence.SchoolSubject.FormationYear
property (from Absence
) But I got an error message his summary that Absence.SchoolSubject
and Absence.SchoolSubject.FormationYear
properties are Null
and not loaded.
What I tried:
Context.ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.TrackAll;
IQueryable<Student> query = Context.Set<Student>().Where( s => s.Id == pkValue );
query.Include( s => s.Group ).Include( s => s.Group.Branche )
.Include( s => s.Group.FormationYear )
.Include( s => s.StudentGuadian ).Include( s => s.Absences )
.Include( s => s.Absences.First().SchoolSubject )
.Include( s => s.Absences.First().SchoolSubject.FormationYear )
.Load();
return query.FirstOrDefault();
Context
is the DbContext
object.
Please I know that is not the correct way to load the Absence
nested navigation properties for a Student
object, please any help to fix this issue?
IQueryable implemenation is immutable, so, you have to store new query. Use ThenInclude
for collection navigations. I have also removed not needed includes:
query = query
.Include( s => s.Group.Branche )
.Include( s => s.Group.FormationYear )
.Include( s => s.StudentGuadian )
.Include( s => s.Absences )
.ThenInclude( s => s.SchoolSubject.FormationYear );
return query.FirstOrDefault();
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.