efcore icon indicating copy to clipboard operation
efcore copied to clipboard

Asynchronous projecting entities with child collection properties

Open erbatista opened this issue 9 years ago • 1 comments

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>.

erbatista avatar Aug 07 '17 18:08 erbatista

duplicate of #9128

smitpatel avatar Aug 07 '17 23:08 smitpatel