運行ABP(asp.net core 3.X+Vue)提示'OFFSET' 附近有語法錯誤。 在 FETCH 語句中選項 NEXT 的用法無效。


創建ASP.NET Boilerplate,還原數據庫和啟動客戶端

這里就略過,具體參考

ABP框架(asp.net core 2.X+Vue)模板項目學習之路(一)

ASP.NET Boilerplate VueJS Template

 

選擇

Target Version:v3.x

Target Framework:.NET Core (Cross Platform)

Single Page Web Application:vue.js

Choose your project's name:MyCompany.MyProject

 

用戶名:admin  密碼:123qwe 點擊登錄后成功進入系統,點擊租戶頁面,提示錯誤:對不起,在處理您的請求期間,產生了一個服務器內部錯誤!

 

 

 

請求信息:

 

 

 

查看日志信息,路徑5.0.0\aspnet-core\src\MyCompany.MyProject.Web.Host\App_Data\Logs:

ERROR 2019-12-13 12:47:27,121 [21   ] Mvc.ExceptionHandling.AbpExceptionFilter - 'OFFSET' 附近有語法錯誤。
在 FETCH 語句中選項 NEXT 的用法無效。
Microsoft.Data.SqlClient.SqlException (0x80131904): 'OFFSET' 附近有語法錯誤。
在 FETCH 語句中選項 NEXT 的用法無效。
   at Microsoft.Data.SqlClient.SqlCommand.<>c.<ExecuteDbDataReaderAsync>b__164_0(Task`1 result)
   at System.Threading.Tasks.ContinuationResultTaskFromResultTask`2.InnerInvoke()
   at System.Threading.Tasks.Task.<>c.<.cctor>b__274_0(Object obj)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
--- End of stack trace from previous location where exception was thrown ---
   at System.Threading.Tasks.Task.ExecuteWithThreadLocal(Task& currentTaskSlot, Thread threadPoolThread)
--- End of stack trace from previous location where exception was thrown ---
   at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReaderAsync(RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReaderAsync(RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReaderAsync(RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.Query.RelationalShapedQueryCompilingExpressionVisitor.AsyncQueryingEnumerable`1.AsyncEnumerator.MoveNextAsync()
   at Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.ToListAsync[TSource](IQueryable`1 source, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.ToListAsync[TSource](IQueryable`1 source, CancellationToken cancellationToken)
   at Abp.Application.Services.AsyncCrudAppService`8.GetAllAsync(TGetAllInput input)
   at Abp.Threading.InternalAsyncHelper.AwaitTaskWithPostActionAndFinallyAndGetResult[T](Task`1 actualReturnValue, Func`1 postAction, Action`1 finalAction)
   at lambda_method(Closure , Object )
   at Microsoft.Extensions.Internal.ObjectMethodExecutorAwaitable.Awaiter.GetResult()
   at Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.AwaitableObjectResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeActionMethodAsync>g__Awaited|12_0(ControllerActionInvoker invoker, ValueTask`1 actionResultValueTask)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeNextActionFilterAsync>g__Awaited|10_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Rethrow(ActionExecutedContextSealed context)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeInnerFilterAsync>g__Awaited|13_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeNextExceptionFilterAsync>g__Awaited|25_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
ClientConnectionId:3cb2fb89-0c31-4bfa-93f7-6b70a7a6ea72
Error Number:102,State:1,Class:15

 

原因是ef core默認生成的分頁sql語句帶OFFSET和FETCH,2012和以后的數據庫支持OFFSET和FETCH,2012以下版本不支持。否則會報錯:'OFFSET' 附近有語法錯誤。 在 FETCH 語句中選項 NEXT 的用法無效。

EXEC sp_executesql N'SELECT [a].[Id], [a].[ConnectionString], [a].[CreationTime], [a].[CreatorUserId], [a].[DeleterUserId], [a].[DeletionTime], [a].[EditionId], [a].[IsActive], [a].[IsDeleted], [a].[LastModificationTime], [a].[LastModifierUserId], [a].[Name], [a].[TenancyName] FROM [AbpTenants] AS [a] WHERE (@__ef_filter__p_0 = CAST(1 AS bit)) OR ([a].[IsDeleted] <> CAST(1 AS bit)) ORDER BY [a].[Id] DESC OFFSET @__p_0 ROWS FETCH NEXT @__p_1 ROWS ONLY', N'@__ef_filter__p_0 bit,@__p_0 int,@__p_1 int', @__ef_filter__p_0 = 0, @__p_0 = 0, @__p_1 = 10

 

修改5.0.0\aspnet-core\src\MyCompany.MyProject.EntityFrameworkCore\EntityFrameworkCore\MyProjectDbContextConfigurer文件如下即可:

using System.Data.Common;
using Microsoft.EntityFrameworkCore;

namespace MyCompany.MyProject.EntityFrameworkCore
{
    public static class MyProjectDbContextConfigurer
    {
        public static void Configure(DbContextOptionsBuilder<MyProjectDbContext> builder, string connectionString)
        {
            builder.UseSqlServer(connectionString);
        }

        public static void Configure(DbContextOptionsBuilder<MyProjectDbContext> builder, DbConnection connection)
        {
            builder.UseSqlServer(connection);
        }
    }
}

改為

using System.Data.Common;
using Microsoft.EntityFrameworkCore;

namespace MyCompany.MyProject.EntityFrameworkCore
{
    public static class MyProjectDbContextConfigurer
    {
        public static void Configure(DbContextOptionsBuilder<MyProjectDbContext> builder, string connectionString)
        {
            builder.UseSqlServer(connectionString, b => b.UseRowNumberForPaging());
        }

        public static void Configure(DbContextOptionsBuilder<MyProjectDbContext> builder, DbConnection connection)
        {
            builder.UseSqlServer(connection, b => b.UseRowNumberForPaging());
        }
    }
}

 

但修改完,還是提示一樣的錯誤

去EFCore的issues

https://docs.microsoft.com/en-us/ef/core/what-is-new/ef-core-3.0/breaking-changes#urn

https://github.com/aspnet/EntityFrameworkCore/issues/16400

EF Core 3.0 從 Preview 6 開始不支持UseRowNumberForPaging,

居然因為sql server 2008不再長期支持EF Core 3.0要舍棄sql server 2008,真是個大坑,難道要升級個sql server 2012?

嘗試降到EF Core 3.0-preview5.19227.1,運行報錯。

 

目前解決的方法

1、升級數據庫到2012

2、選擇Target Version:v2.x

 我選擇了后者:

加上UseRowNumberForPaging,打開租戶頁面正常

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM