'OFFSET' 附近的语法不正确。 FETCH 语句中选项 NEXT 的使用无效 "in Entity Framework core"

Incorrect syntax near 'OFFSET'. Invalid usage of the option NEXT in the FETCH statement "in Entity Framework core"

这是我的 EF 核心代码:

 int page = 1, rowPerPage = 5;
 int count = ctx.Specialty.Count();
 int start = page * rowPerPage;

 var Select = ctx.Specialty.OrderByDescending(u => u.IdS)
            .Skip(start)
            .Take(rowPerPage)
            .AsEnumerable();

错误:

Incorrect syntax near 'OFFSET'. Invalid usage of the option NEXT in the FETCH statement

sql 服务器 2008 不支持我的查询

解决方法:

public class AppDbContext : DbContext
{

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        var coonectionString = "Data Source=localhost\MSSQLSERVER01;Initial Catalog=AppDb01;Integrated Security=True";
        optionsBuilder.UseSqlServer(coonectionString);
    }
}

目标服务器的值连接字符串 并且还注入了设置,示例代码是默认的ASP.NET Core项目格式。

为此有一个兼容性设置 (UseRowNumberForPaging),可以在 DbContext 本身中配置:

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        var coonectionString = "Data Source=localhost\MSSQLSERVER01;Initial Catalog=AppDb01;Integrated Security=True";
        optionsBuilder.UseSqlServer(coonectionString, builder => builder.UseRowNumberForPaging());
    }

或作为启动的一部分:

    public void ConfigureServices(IServiceCollection services)
    {
        var coonectionString = "Data Source=localhost\MSSQLSERVER01;Initial Catalog=AppDb01;Integrated Security=True";
        services.AddDbContext<AppDbContext>(options => options.UseSqlServer(coonectionString, builder => builder.UseRowNumberForPaging()));
    }

UseRowNumberForPaging was removed 在 EF Core 3.x 中,方法被标记为已过时。但是,您可以改用 EfCore3.SqlServer2008Query 包。 Nuget 中有 2 个包可用,一个适用于 >= .NET 5.0,另一个适用于 >= .NET Core 3.1

用法:

 services.AddDbContext<MyDbContext>(o => 
       o.UseSqlServer(Configuration.GetConnectionString("Default"))
        .ReplaceService<IQueryTranslationPostprocessorFactory, SqlServer2008QueryTranslationPostprocessorFactory>());

致所有使用 .Net 6 的人 我找到了这个包 EntityFrameworkCore.UseRowNumberForPaging 0.3.0: https://www.nuget.org/packages/EntityFrameworkCore.UseRowNumberForPaging/

基于 github 问题:https://github.com/dotnet/efcore/issues/16400

谢谢Rwing,无论你是谁,你都是英雄!