efcore
efcore copied to clipboard
Asynchronous projecting entities with child collection properties
When projecting a result which have a collection property it will deadlock.
public class FooModel
{
public Guid Id { get; set; }
public string Name { get; set; }
public List<ChildModel> Childs { get; set; }
}
public static async Task<List<FooModel>> ListFooModelAsync()
{
using (var db = new AppDbContext())
{
var foo_items = await db.Foos
.Include(e => e.Childs)
.Select(e => new FooModel
{
Id = e.Id,
Name = e.Name,
Childs = e.Childs.Select(
child => new ChildModel { Id = child.Id, Name = child.Name })
.ToList() //Cause deadlock
})
.ToListAsync()
.ConfigureAwait(false);
return foo_items;
}
}
By removing ToList(), the deadlock occurs as soon as I try to use the result, in this case Childs type will be Microsoft.EntityFrameworkCore.Query.Internal.AsyncLinqOperatorProvider.EnumerableAdapter<ChildModel>.
duplicate of #9128