2019年12月20日 星期五

.Net Core 3.0以上版本分頁問題

今天寫了Blazor的Sample專案測試到了分頁時
 stafflist = await this._staffService.GetAll().Skip(1).Take(1).ToListAsync();

出現了下列錯誤
SqlException: 接近 'OFFSET' 之處的語法不正確。 FETCH 陳述式中的選項 NEXT 使用方式無效。 context
後來找到一堆方法
services.AddDbContext<CoreSampleContext>(options => options.UseSqlServer(ConnectionString));
改成
services.AddDbContext<CoreSampleContext>(options => options.UseSqlServer(ConnectionString,build =>build.UseRowNumberForPaging()));
很好一樣出錯,後來找到了原因,目前使用的資料庫是SQL Server 2008R2
微軟提到了已移除 UseRowNumberForPaging 目前可能只有兩條路
1.升級SQL Server
2.降低.Net Core版本
3.Dapper或其他可直接下SQL語法