Skip and Take not working with MySQL EntityFrameworkCore

后端 未结 2 524
旧时难觅i
旧时难觅i 2020-12-20 19:57

I am trying to paginate Products from MySQL db, but if I use Skip() or Take() it returns an empty Json array as my web api response li

相关标签:
2条回答
  • 2020-12-20 20:26

    It turned out to be a bug in MySql.Data EF connector provided by Oracle, bug details is posted here.

    Alternative solution:

    I changed to another connector called Pomelo, now Skip and Take works perfectly fine. You can search nuget for Pomelo.EntityFrameworkCore.MySql and install appropriate version for your project.

    To use, simply change .UseMySQL to .UseMySql when configuring DbContext, as oracle connector use SQL and pomelo use Sql only casing is different.

    services.AddDbContext<ApplicationDbContext>(options =>
        options.UseMySql(Configuration.GetConnectionString("DefaultConnection")));
    
    0 讨论(0)
  • 2020-12-20 20:34

    Well I knwon the answer is Old ... but ... in EF mysql you need do pass one order by or order by desc before the skip.

    Real solution, not alternative:

    like this:

    var yourVar = dbContext.LinkText
                           .Where(x => x.active)
                           .OrderByDescending(x => x.startDate)
                           .Skip(50)
                           .Take(10);
    

    You can use with any logic on skip and take like this :

    query
    .OrderByDescending(x => x.startDate)
    .Skip(page <= 1 ? 0 : (page - 1) * (qty == 0 ? 10 : qty))
    .Take(qty == 0 ? 10 : qty);
    

    and then the mySQL will recieve the code:

    *...the query...*     
    ORDER BY  `Extent1`.`startDate` DESC 
         LIMIT 0,10
    
    0 讨论(0)
提交回复
热议问题