使用 EntityFrameworkCore 链接 SQL Server 2008 执行.Skip().Take()
分页查询时出现以下异常:c#
SqlException: 'OFFSET' 附近有语法错误。 在 FETCH 语句中选项 NEXT 的用法无效。
SQL Server 中有几种实现分页的方式,可是有版本限制.ide
而在EntityFramworkCore
中默认使用的是最新的offset fetch
方式,
而我使用的 SQL Server 2008 不支持该关键字, 天然就异常.fetch
这是 EntityFrameworkCore 中的原话.ui
Use a ROW_NUMBER() in queries instead of OFFSET/FETCH. This method is backwards-compatible to SQL Server 2005. public virtual void UseRowNumberForPaging();code
因而乎,咱们指定下 EntityFramworkCore 中使用的分页模式便可.ip
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { optionsBuilder.UserSqlServer("yourConnectionString", options => { options.UseRowNumberForPaging(); }); }