Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The provider for the source IQueryable doesn't implement IDbAsyncQueryProvider

I'm intending to create pagination on my api using the following example: https://www.codeproject.com/Articles/1073488/Paging-in-ASP-NET-Web-API I've implemented all code and its running until I hit the following line of code:

var results = await projection.ToListAsync();

then it causes a runtime error:

"The provider for the source IQueryable doesn't implement IDbAsyncQueryProvider..."

I've researched this error and tried to initialize the Mapper in my Global.asax.cs file like so:

        protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();
        Mapper.Initialize(cfg => cfg.CreateMap<Logging.Models.LogsModel, Log>());
        WebApiConfig.Register(GlobalConfiguration.Configuration);
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);

    }

This doesn't resolve the problem, does anybody know what I'm doing wrong initializing Automapper?

Thanks in Advance.

Here is the implementing code in my controller:

        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);
        }
    }

and heres the definition of GetLogs:

        public IEnumerable<LogsModel> GetLogs()
    {
        List<LogsModel> logs = Logs.ToList<LogsModel>();
        return logs;
    }
like image 561
Lyle Avatar asked Nov 02 '25 00:11

Lyle


1 Answers

I had a similar problem, the problem was that I used System.Data.Entity instead of Microsoft.EntityFrameworkCore

I just replaced:

using System.Data.Entity; with using Microsoft.EntityFrameworkCore;

Because both namespaces has equal methods for example SingleOrDefaulAsync, but in System.Data.Entity this method has InvalidOperationException source doesn't implement IDbAsyncQueryProvider see more here https://learn.microsoft.com/en-us/dotnet/api/system.data.entity.queryableextensions.singleordefaultasync?view=entity-framework-6.2.0#System_Data_Entity_QueryableExtensions_SingleOrDefaultAsync__1_System_Linq_IQueryable___0__System_Linq_Expressions_Expression_System_Func___0_System_Boolean___System_Threading_CancellationToken_

like image 112
AudioField Avatar answered Nov 03 '25 14:11

AudioField