Entity Framework - ASP.NET Core Include function returns nested relationships

asp.net-core asp.net-web-api c#-3.0 entity-framework-core sql-server

Question

The Include() method should usually return the list of specific objects but strangely, it returns all its nested objects without even using ThenInclude(). How should I block these nested objects?

return Db.Courses.Include(x => x.Program).ToList();

This should return the list of courses with program objects but it also returns the course objects that are also present in the Programs object. How to do I break this loop?

Courses Class

public Courses()
    {
        CourseCoReqsCoRequisiteCourse = new HashSet<CourseCoReqs>();
        CourseCoReqsCourse = new HashSet<CourseCoReqs>();
        CourseGroupCourses = new HashSet<CourseGroupCourses>();
        CoursePreReqsCourse = new HashSet<CoursePreReqs>();
        CoursePreReqsPreRequisiteCourse = new HashSet<CoursePreReqs>();
        CurriculumCoursePrerequisite = new HashSet<CurriculumCoursePrerequisite>();
        EquivalenceCourses = new HashSet<EquivalenceCourses>();
        SemesterCourses = new HashSet<SemesterCourses>();
        StudentCourses = new HashSet<StudentCourses>();
    }

    public int CourseId { get; set; }
    public string CourseNumber { get; set; }
    public string Title { get; set; }
    public string Description { get; set; }
    public int Credits { get; set; }
    public bool? NormallyOfferedFall { get; set; }
    public bool? NormallyOfferedSpring { get; set; }
    public bool? NormallyOfferedSummer { get; set; }
    public bool? NormallyOfferedWinterInt { get; set; }
    public bool? NormallyOfferedSpringInt { get; set; }
    public bool? NormallyOfferedSummerInt { get; set; }
    public bool? IsActive { get; set; }
    public bool? IsElective { get; set; }
    public int ProgramId { get; set; }
    public string Rules { get; set; }

    public virtual Programs Program { get; set; }
    public virtual ICollection<CourseCoReqs> CourseCoReqsCoRequisiteCourse { get; set; }
    public virtual ICollection<CourseCoReqs> CourseCoReqsCourse { get; set; }
    public virtual ICollection<CourseGroupCourses> CourseGroupCourses { get; set; }
    public virtual ICollection<CoursePreReqs> CoursePreReqsCourse { get; set; }
    public virtual ICollection<CoursePreReqs> CoursePreReqsPreRequisiteCourse { get; set; }
    public virtual ICollection<CurriculumCoursePrerequisite> CurriculumCoursePrerequisite { get; set; }
    public virtual ICollection<EquivalenceCourses> EquivalenceCourses { get; set; }
    public virtual ICollection<SemesterCourses> SemesterCourses { get; set; }
    public virtual ICollection<StudentCourses> StudentCourses { get; set; }
}

Programs class:

 public partial class Programs
{
    public Programs()
    {
        Courses = new HashSet<Courses>();
        Curricula = new HashSet<Curricula>();
        People = new HashSet<People>();
        ProgramCourseGroups = new HashSet<ProgramCourseGroups>();
        Roles = new HashSet<Roles>();
    }

    public int ProgramId { get; set; }
    public int DepartmentId { get; set; }
    public string ProgramCode { get; set; }
    public string ProgramName { get; set; }
    public bool? IsActive { get; set; }
    public bool? IsMinor { get; set; }

    public virtual Departments Department { get; set; }
    public virtual ICollection<Courses> Courses { get; set; }
    public virtual ICollection<Curricula> Curricula { get; set; }
    public virtual ICollection<People> People { get; set; }
    public virtual ICollection<ProgramCourseGroups> ProgramCourseGroups { get; set; }
    public virtual ICollection<Roles> Roles { get; set; }
}
1
0
3/18/2020 11:07:29 PM

Popular Answer

This should return the list of courses with program objects but it also returns the course objects that are also present in the Programs object. How to do I break this loop?

Turn off Change Tracking, and, of course, make sure Lazy Loading is disabled. EG

return Db.Courses
         .AsNoTracking()
         .Include(x => x.Program)
         .ToList();

The Change Tracker fixes up Navigation Properties as it loads objects.

0
3/18/2020 11:22:12 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow