From 90540e4fb4d79f6c408342158867e6dce657c946 Mon Sep 17 00:00:00 2001 From: bicijinlian Date: Sat, 8 Jul 2023 16:32:20 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=AF=B9SQL2008=E5=88=86?= =?UTF-8?q?=E9=A1=B5=E7=9A=84=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Docs/说明.md | 15 ++++++++++++++- EFCore7Study.DataService/AppDbContext.cs | 4 +++- .../EFCore7Study.DataService.csproj | 1 + 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/Docs/说明.md b/Docs/说明.md index ce19423..cceb8c6 100644 --- a/Docs/说明.md +++ b/Docs/说明.md @@ -57,4 +57,17 @@ public class AppDbContext : DbContext public DbSet Accounts { get; set; } } -``` \ No newline at end of file +``` + +## SQL Server 2008 分页错误 +原因:EF Core 放弃了对SQL2008的支持,特别是分页查询会报错:"OFFSET 附近有语法错误",原因是OFFSET的分页语法,从SQL Server 2012开始支持。 + +解决:使用第三方库 "EntityFrameworkCore.UseRowNumberForPaging", 在SQL 2008中改用 Row_Number() 分页. 如下方式配置项目使用:加一个 .UseRowNumberForPaging() + +```csharp +//原使用方法 + xx.UseSqlServer(ConnectString, opt => {}) + +//修改后的使用方法 + .UseSqlServer(ConnectString, opt => { opt.UseRowNumberForPaging(); }) +``` diff --git a/EFCore7Study.DataService/AppDbContext.cs b/EFCore7Study.DataService/AppDbContext.cs index d1922ef..19cbaec 100644 --- a/EFCore7Study.DataService/AppDbContext.cs +++ b/EFCore7Study.DataService/AppDbContext.cs @@ -3,6 +3,8 @@ using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.DependencyInjection; +using EntityFrameworkCore.UseRowNumberForPaging; + namespace EFCore7Study.DataService { /// @@ -36,7 +38,7 @@ namespace EFCore7Study.DataService } optionsBuilder - .UseSqlServer(ConnectString) + .UseSqlServer(ConnectString, opt => { opt.UseRowNumberForPaging(); }) .EnableSensitiveDataLogging(); } } diff --git a/EFCore7Study.DataService/EFCore7Study.DataService.csproj b/EFCore7Study.DataService/EFCore7Study.DataService.csproj index d462168..bc0ab51 100644 --- a/EFCore7Study.DataService/EFCore7Study.DataService.csproj +++ b/EFCore7Study.DataService/EFCore7Study.DataService.csproj @@ -7,6 +7,7 @@ +