我打算使用以下示例在我的api上创建分页:https://www.codeproject.com/Articles/1073488/Paging-in-ASP-NET-Web-API 我已经实现了所有代码及其运行,直到我点击以下代码行:
var results = await projection.ToListAsync();
然后它会导致运行时错误:
“源IQueryable的提供程序未实现IDbAsyncQueryProvider ...”
我研究过这个错误并试图在我的Global.asax.cs文件中初始化Mapper,如下所示:
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
Mapper.Initialize(cfg => cfg.CreateMap<Logging.Models.LogsModel, Log>());
WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
}
这不能解决问题,是否有人知道我在初始化Automapper时做错了什么?
先谢谢。
以下是我的控制器中的实现代码:
public async Task<IHttpActionResult> Get(int? page = null, int pageSize = 10, string orderBy = nameof(LogsModel.Id), bool ascending = true)
{
if (page == null)
{
return Ok(_repository.GetLogs());
}
else
{
var returnList = _repository.GetLogs().AsQueryable();
returnList = returnList as IQueryable<LogsModel>;
var logsout = await CreatePagedResults<LogsModel, Log>(returnList, page.Value, pageSize, orderBy, ascending);
return Ok(logsout);
}
}
并且继承了GetLogs的定义:
public IEnumerable<LogsModel> GetLogs()
{
List<LogsModel> logs = Logs.ToList<LogsModel>();
return logs;
}