最近在做ASPNET CORE WEBAPI的时候遇到了一个关于EntityFramework Core异步方式执行查询(ToListAsync、FirstAsync、ForEachAsync等)的问题。
在此记录以下解决方法,当然如果大佬们有其它更好的也可以下面留言

样例代码:

var result = await _context.Company.AsNoTracking()
            .Where(m => m.ID == parameters.id)
            .ProjectTo<CompanyDto>(_mapper.ConfigurationProvider)
            .ToListAsync();

错误信息:

  The provider for the source IQueryable doesn't implement IDbAsyncQueryProvider.Only providers that implement IDbAsyncQueryProvider can be used for Entity Framework asynchronous operations

解决方案:

  方法一:

    官方文档给出了一个解决方案 : http://go.microsoft.com/fwlink/?LinkId=287068

  方法二:

var result = await Task.FromResult(_context.Company.AsNoTracking()
            	.Where(m => m.ID == parameters.id)
            	.ProjectTo<CompanyDto>(_mapper.ConfigurationProvider)
            	.ToList());

相关文章:

  • 2022-12-23
  • 2021-11-11
  • 2022-12-23
  • 2021-10-05
  • 2022-12-23
  • 2021-11-09
  • 2021-08-04
  • 2021-08-31
猜你喜欢
  • 2022-02-18
  • 2022-12-23
  • 2022-02-17
  • 2022-01-28
  • 2021-05-12
  • 2021-10-11
  • 2021-05-30
相关资源
相似解决方案