.Net EF Core数据库使用SQL server 2008 R2分页报错How to avoid the “Incorrect syntax near 'OFFSET'. Invalid usa

1、  问题说明git

最近.Net EF core 程序部署到服务器,服务器数据库安装的是SQL server 2008 R2,我本地用的的是SQL server 2014,在用到分页查询时报错以下:github

How to avoid the “Incorrect syntax near 'OFFSET'. Invalid usage of the option NEXT in the FETCH statement.”数据库

经过问题描述能够分析是数据库SQL server 2008 R2版本SQL语句不支持关键字OFFSET,NEXT,由于这两个关键字是SQL server 2012之后的新特性。服务器

2、  解决方案ui

因为我采用的是.Net core EF code first访问数据库,在网上查找如何制定数据库版本,没有太多有用的资料。最后在EntityFrameworkCore官方开源github issue里找到了解决方案,由于已经有人先遇到这个问题了。spa

Github issue链接地址:https://github.com/aspnet/EntityFrameworkCore/issues/4616code

经过配置.UseRowNumberForPaging() 即配置用row number SQL关键字进行分页,详细代码以下:server

public static class MyDBContextConfigurer
{
        public static void Configure(DbContextOptionsBuilder<MyDBContext> builder, string connectionString)
        {
            builder.UseSqlServer(connectionString, option => option.UseRowNumberForPaging() );
        }

        public static void Configure(DbContextOptionsBuilder<MyDBContext> builder, DbConnection connection)
        {
            builder.UseSqlServer(connection, option => option.UseRowNumberForPaging());
        }}
相关文章
相关标签/搜索