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 @@ +