Multi-async in Entity Framework 6?

前端 未结 3 1953
说谎
说谎 2020-11-28 07:54

This is my code:

var banner = context.Banners.ToListAsync()
var newsGroup = context.NewsGroups.ToListAsync()
await Task.WhenAll(banner, newsGroup);
         


        
3条回答
  •  伪装坚强ぢ
    2020-11-28 08:28

    The exception explains clearly that there is only one asynchronous operation per context allowed at a time.

    So, you either have to await them one at a time as the error message suggests:

    var banner = await context.Banners.ToListAsync();
    var newsGroup = await context.NewsGroups.ToListAsync();
    

    Or you can use multiple contexts:

    var banner = context1.Banners.ToListAsync();
    var newsGroup = context2.NewsGroups.ToListAsync();
    await Task.WhenAll(banner, newsGroup);
    

提交回复
热议问题