locked
Eager loading goes into "hyperloop" in EFCore and .Net Core Web API, Generic Repo RRS feed

  • Question

  • User360451555 posted

    Am trying to fetch data in a .Net Core Web API using eager loading on a many to many relationship. But when i inspect the the point where i set the breakpoint in a Get method, i see an endless fetch of Orders that are fetching groups which fetch again orders that fetch again groups and so on and so on...

    Here is my Repo function that am using

            public async Task<IList<T>> GetAllIncludeAsync(bool disableTracking, Func<IQueryable<T>, IIncludableQueryable<T, object>> navigationProperties)
            {
                IQueryable<T> dbQuery = _context.Set<T>();
    
                if (disableTracking)
                {
                    dbQuery = dbQuery.AsNoTracking();
                }
    
                if (navigationProperties != null)
                {
                    dbQuery = navigationProperties(dbQuery);
                }
    
                return await dbQuery.ToListAsync();
            }

    Here is the Orders entity

    public class Order
        {
            public Order()
            {
                OrderGroups = new List<OrderGroups>();
                OrderArticles = new List<OrderArticle>();
            }
    
            [Key]
            [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
            public int OrderId { get; set; }
    
            [Required]
            public string OrderNumber { get; set; }
    
            public ICollection<OrderGroups> OrderGroups { get; set; }
    
            public ICollection<OrderArticle> OrderArticles { get; set; }
        }

    Here is the Groups Entity

    public class Group
        {
            [Key]
            [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
            public int GroupId { get; set; }
    
            [Required]
            public string GroupName { get; set; }
    
            public virtual ICollection<User> Users { get; set; }
    
            public int? ParentGroupId { get; set; }
    
            [ForeignKey("ParentGroupId")]
            public Group Parent { get; set; }
    
            public int OrganizationId { get; set; }
    
            [ForeignKey("OrganizationId")]
            public Organization Organization { get; set; }
    
            public ICollection<OrderGroups> GroupOrders { get; set; }
        }

    And here is the connecting Table OrderGroups

        public class OrderGroups
        {
            public int GroupId { get; set; }
    
            [ForeignKey("GroupId")]
            public Group Group { get; set; }
    
            public int OrderId { get; set; }
    
            [ForeignKey("OrderId")]
            public Order Order { get; set; }
        }

    When i ran a code like this

    var returnedList = await _unitOfWork.Repository<Order>().GetAllIncludeAsync(
                        true, 
                        x => x.Include(og => og.OrderGroups).ThenInclude(k => k.Group));
    // Here i set a breakpoint to inspect further var xx = returnedList.ToList(); return Ok(xx);

    Here is a view of my watch window snippet

    Description_1

    Either i've got my relationships wrong or somewhere in the code its not going well. Coz even when the request executes successfully, i get a broken json result as the one below

    PostMan

    Tuesday, April 2, 2019 6:56 PM

All replies