0

I need to make a recursive call with includes to a database (EF 6.2.0), is it possible to do it in a generic way?

public class Option
{
    public long OptionID { get; set; }

    public long Property1 { get; set; }

    public long Property2 { get; set; }

    public long Property3 { get; set; }

    public long Property4 { get; set; }
}

public class ClassOne
{
    public long OrderID { get; set; }

    public long OptionID { get; set; }

    public long Property1 { get; set; }

    public long Property2 { get; set; }

    public long Property3 { get; set; }

    public long Property4 { get; set; }

    public virtual Option Option { get; set; }

    public virtual ICollection<ClassOne> CollectionOne { get; set; } = new HashSet<ClassOne>();

    public virtual ICollection<ClassTwo> CollectionTwo { get; set; } = new HashSet<ClassTwo>();
}

    public ICollection<TEntity> Find(Expression<Func<TEntity, bool>> currentExpression, IncludeProperties includeProperties)
    {
        using (var currentContext = new TContext())
        {
            return (includeProperties == IncludeProperties.None
                    ? new List<Expression<Func<TEntity, object>>>()
                    : PropertyInfoToExpression(GetVirtualProperties(new TEntity())))
                .Aggregate(currentContext.Set<TEntity>().AsQueryable(),
                    (x, includeProperty) => x.Include(includeProperty)).Where(currentExpression).ToList();
        }
    }

_classOneRepository.Find(x => x.Property1 == 1), IncludeProperties.All);

With this code I get all the collections of the parent item, but I can not get the data of collections of their children.

avechuche
  • 1,470
  • 6
  • 28
  • 45

1 Answers1

0

I need to make a recursive call with includes to a database (EF 6.2.0), is it possible

Simply not possible. Eager loading using "include" generates one big query to fetch all the related Entities and EF has no way to express a recursive query using a .NET Expression, or generate one in SQL.

Either recurse through the results and using Explicit Loading or Lazy Loading fetch the levels of children, or load all the Entities and let the Change Tracker "fix up" the navigation properties. If your back-end supports recursive queries you could write a Store Query to fetch the tree of entities for the Change Tracker to knit together.

David Browne - Microsoft
  • 80,331
  • 6
  • 39
  • 67