Get all children recursively in Entity Framework Core In Entity Framework Core we can have recursive entities. But we cannot do an "Include" for these recursives (can we?). So I wrote a Get-method that does it for me:

First, this is my RecursiveEntity&lt;T&gt; base-class:

public class Entity : IEntity
{
    [Key]
    public int Id { get; set; }
}

public abstract class RecursiveEntity&lt;TEntity&gt; : Entity, IRecursiveEntity&lt;TEntity&gt; 
    where TEntity : RecursiveEntity&lt;TEntity&gt;
{
    public virtual TEntity Parent { get; set; }
    public virtual ICollection&lt;TEntity&gt; Children { get; set; }
}


And this is my Repository-base-class for recurisve entities:

public abstract class RecursiveRepository&lt;T, TDataContext&gt; 
    where T : RecursiveEntity&lt;T&gt; where TDataContext : DbContext
{
    protected IEnumerable&lt;T&gt; Get(Expression&lt;Func&lt;T, bool&gt;&gt; expression)
    {
        IQueryable&lt;T&gt; parents = DataContext.Set&lt;T&gt;()
            .Include(x =&gt; x.Children)
            .Where(w =&gt; w.Parent == null)
            .Where(expression);

        foreach (T entity in parents)
        {
            if (entity.Children != null &amp;&amp; entity.Children.Any())
                entity.Children = _getChildren(entity, expression).ToList();

            yield return entity;
        }
    }

    private IEnumerable&lt;T&gt; _getChildren(T parentEntity, Expression&lt;Func&lt;T, bool&gt;&gt; expression)
    {
        IQueryable&lt;T&gt; children = DataContext.Set&lt;T&gt;()
            .Include(x =&gt; x.Parent)
            .Where(w =&gt; w.Parent != null &amp;&amp; w.Parent.Id == parentEntity.Id)
            .Where(expression);

        foreach (T entity in children)
        {
            entity.Children = _getChildren(entity, expression).ToList();
            yield return entity;
        }
    }
}


Any suggestion what I can do better? 
Is it a good solution to do that many DataContext&lt;Set&gt;-accesses and assign them  to the parent with ToList()?
If you remove virtual from Children it'll work with Include(e =&gt; e.Children):

  public abstract class RecursiveEntity&lt;TEntity&gt;
    : Entity, IRecursiveEntity&lt;TEntity&gt;
    where TEntity : RecursiveEntity&lt;TEntity&gt;
  {
    public virtual TEntity Parent { get; set; }
    public ICollection&lt;TEntity&gt; Children { get; set; }
  }


The meaning with virtual is to provide lazy loading. If you can't live with that, you'll have to do it manually, as you do.



Alternatively I think, you can add "MultipleActiveResultSets=True" to your connection string.



UPDATE

I seems that EF 6 and EFCore work differently when it comes to Include?

EFCore has the Include() and ThenInclude pattern but that is rather useless for recursive initialization.

When loading manually have you then experimented with the abilities to load navigation properties on each object as in:

public IEnumerable&lt;TEntity&gt; Get&lt;TEntity&gt;(Expression&lt;Func&lt;TEntity, bool&gt;&gt; filter) where TEntity: RecursiveEntity&lt;TEntity&gt;
{
  foreach (TEntity entity in Set&lt;TEntity&gt;().Where(e =&gt; e.Parent == null).Where(filter))
  {
    GetChildren(entity, filter);
    yield return entity;
  }
}

private void GetChildren&lt;TEnity&gt;(TEnity parent, Expression&lt;Func&lt;TEnity, bool&gt;&gt; childFilter) where TEnity : RecursiveEntity&lt;TEnity&gt;
{
  Entry(parent).Collection(e =&gt; e.Children).Query().Where(childFilter).Load();
  // Entry(parent).Reference(e =&gt; e.Parent).Load(); // I think this shouldn't be necessary because loading the children will load the parent on them

  if (parent.Children != null)
  {
    foreach (TEnity child in parent.Children)
    {
      GetChildren(child, childFilter);
    }
  }
}


It should produce the same result as yours, but is maybe a little clearer and in line with the EF concept.