.Net EF Core数据库使用SQL server 2008 R2分页报错How to avoid the “Incorrect syntax near 'OFFSET'. Invalid usage of the option NEXT in the FETCH statement.”
2018-03-24 13:48
676 查看
一、 问题说明
最近.Net EF core 程序部署到服务器,服务器数据库安装的是SQL server 2008 R2,我本地用的的是SQL server 2014,在用到分页查询时报错如下:
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以后的新特性。
二、 解决方案
由于我采用的是.Net core EF code first访问数据库,在网上查找如何制定数据库版本,没有太多有用的资料。最后在EntityFrameworkCore官方开源github issue里找到了解决方案,因为已经有人先遇到这个问题了。
Github issue连接地址:https://github.com/aspnet/EntityFrameworkCore/issues/4616
通过配置.UseRowNumberForPaging() 即配置用row number SQL关键字进行分页,详细代码如下:
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()); }
}
相关文章推荐
- CS 420/520: Data Communication--Spring Semester 2008 at the University of Idaho in Moscow
- ie does not support the event of the option in select control
- How to Set Up SQL Server 2008 FileStream In the Cluster
- The usage of intellisense in Vs .net 2005
- Eclipse和Maven的结合使用--Please make sure the -vm option in eclipse.ini
- Scored a good score in the Cheat-Test exam 70-432 SQL Server 2008
- sql express 2008 - Failed to generate a user instance of SQL Server due to a failure in starting the process for the user instan
- Warning[Pa082]:undefined behavior: the order of volatile accesses is undefined in this statement
- Eclipse和Maven的结合使用--Please make sure the -vm option in eclipse.ini
- [MSSQL]SQL Server 2008 Using the MERGE statement
- the usage of Properties in java.util
- Ballmer: Investing in the Next Decade of Growth and Opportunity
- SQL Server 2012使用OFFSET/FETCH NEXT分页及性能测试
- SQL Server 2012服务端使用OFFSET/FETCH NEXT实现分页
- Misleading error message in PowerShell script: "Invalid assignment expression. The left hand side of an assignment operator need
- map(映射)的使用方法 (The usage of map.)
- sql server 2008 R2 Error 9003:The log scan number passed to log scan in database is not valid
- How to check the usage of net ports in linux?
- ADODB使用excel的"Cannot modify the design of table 'xxxx'. It is in a read-only database"的问题
- VC助手使用问题————all instance of the license"*" are in use .VA will be disabled