I have a tree of objects whose size is potentially infinite.
The entity Category has other Category entities as children. In other words, a category can have subcategories: the depth is infinite.
Here below you can see a simplified version of my entity
public class ProductCategory : IEntity<ProductCategory>, IDeletable
{
public Guid ProductCategoryId { get; private set; }
public virtual ICollection<ProductCategory> Children { get; set; }
public virtual ProductCategory Father { get; set; }
}
I used to have lazy loading until some hours ago and so I was able to easily get children of children of children.
Now I have disabled lazy loading because I had problems with it, and my query to get the children of a category is the follwing.
public IEnumerable<ProductCategory> GetAllChildrenOfCategory(Guid categoryId)
{
var results = GetAllProductCategoriesQuery()
.Where(elt => elt.FatherId.Equals(categoryId))
.Include(elt => elt.Father)
.Include(elt => elt.Children);
return results.ToList().CloneEachElement();
}
I get all the children of the category. However, I don't have the children of the children of the children...
Now I have two questions:
Is it possible to write a query so that you have the entire tree of categories?
Alternatively, is it possible to configure entity framework so that it always give you the children of category once you extract it from the database, so that I neither have to explictly include the navigation properties?
Other options...?