跳过不使用 MySQL EntityFrameworkCore [英] Skip and Take not working with MySQL EntityFrameworkCore

查看:13
本文介绍了跳过不使用 MySQL EntityFrameworkCore的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从 MySQL 数据库中对 Products 进行分页,但是如果我使用 Skip()Take() 它返回一个空的 Json像这样的数组作为我的 web api 响应

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 like this

[]

但是诸如 FirstOrDefault()Where() 之类的扩展方法...工作正常.这是代码片段:

But extension methods such as FirstOrDefault(), Where() ... works fine. Here's the code snippet:

public IActionResult GetPage(int page, int pageSize = 2)
{            
    int productCount = _context.Products.Count(); // 5
    float totalPages = (float)Math.Ceiling((float)productCount / pageSize); //2.5 -- round to 3

    if (page < 1 || page > totalPages) return NotFound();
    var products = _context.Products.Skip((page - 1) * pageSize).Take(pageSize); //skip & take err mysql ef

    return Ok(products);
}

我什至对查询 .Skip(1).Take(2) 进行了硬编码,但没有运气.任何人都遇到过这个问题或知道解决方法吗?

I even hardcoded the query .Skip(1).Take(2) with no luck. Anyone have faced this problem or know a workaround?

推荐答案

原来是Oracle提供的MySql.DataEF连接器的一个bug,bug详情贴这里.

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

替代解决方案:

我改用另一个名为 Pomelo 的连接器,现在 SkipTake 工作得很好.您可以在 nuget 中搜索 Pomelo.EntityFrameworkCore.MySql 并为您的项目安装适当的版本.

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.

使用时,只需在配置DbContext时将.UseMySQL改为.UseMySql,因为oracle连接器使用SQL和 pomelo 使用 Sql 只是大小写不同.

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

这篇关于跳过不使用 MySQL EntityFrameworkCore的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆