C# 如何加载导航集合';实体对象的属性
Iam使用C# 如何加载导航集合';实体对象的属性,c#,asp.net-core,entity-framework-core,.net-5,C#,Asp.net Core,Entity Framework Core,.net 5,Iam使用实体框架核心从事Asp、net core 5项目目标.net 5 我有以下实体: 学生: [ Key ] public string Id { get; set; } [ Required ] public string FirstName
实体框架核心
从事Asp、net core 5
项目目标.net 5
我有以下实体:
学生:
[ 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; }
[ 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; }
[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; }
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public byte Id { get; set; }
[Required]
public string Title { get; set; }
public virtual ICollection<SchoolSubject> SchoolSubjects { get; set; }
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();
学校主题:
[ 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; }
[ 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; }
[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; }
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public byte Id { get; set; }
[Required]
public string Title { get; set; }
public virtual ICollection<SchoolSubject> SchoolSubjects { get; set; }
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
是DbContext
对象
我知道这不是为
学生对象加载缺勤嵌套导航属性的正确方法,请提供帮助以解决此问题?IQueryable实现是不可变的,因此,您必须存储新查询。
使用然后使用include
进行集合导航。我还删除了不需要的内容,包括:
query=query
.Include(s=>s.Group.Branche)
.包括(s=>s.Group.FormationYear)
.包括(s=>s.StudentGuadian)
.包括(s=>s.缺勤)
。然后包括(s=>s.school subject.FormationYear);
返回query.FirstOrDefault();
非常感谢兄弟,这很有效