【netcore入坑記】UseRowNumberForPaging 分頁報錯 SQL Server 2008 R2 EntityFrameworkCore

異常環境:

netcore版本:.Net Core 2.1

efcore版本:Microsoft.EntityFrameworkCore.SqlServer 2.1.1

sql sqlserver 版本:SQL Server 2008 R2

報錯代碼:

爲了兼容2008數據庫,配置了 RowNumberForPaging

            var optionsBuilder = new DbContextOptionsBuilder<DbObjectContext>();
            optionsBuilder.UseSqlServer(connStr, b => b.UseRowNumberForPaging());
            builder.RegisterType<DbObjectContext>()
                .As<IObjectContext>()
                .WithParameter("options", optionsBuilder.Options)
                .InstancePerLifetimeScope();

查詢

            var query = _menuService.Where(t => t.MenuType == req.TypeID)
                .Where(t => t.IsDel == false)
                .Where(t => t.SchoolId == schoolid);

            var count = query.Count();
            var list = new List<CrmCarouselListItem>();

            if (count > 0)
            {
                var index = req.GetPageIndex();
                var size = req.GetPageSize();

                query = query.OrderBy(t => t.OrderAsc)
                    .Skip((index - 1) * size)
                    .Take(size);

                List<TMenu> rs = null;

                rs = query.ToList();

            }

代碼在ToList()方法報異常,而且是併發查詢偶爾發生

錯誤信息:

 錯誤信息不定,一般是莫名其妙的錯誤或者sql語法錯誤

例如

System.Data.SqlClient.SqlException (0x80131904): 無法綁定由多個部分組成的標識符 "t0.__RowNumber__"

或者字段格式錯誤

An exception occurred while reading a database value for property 'TMenu.ImgUrl'. The expected type was 'System.String' but the actual value was of type 'System.Int32'.

或者莫名的index錯誤

System.IndexOutOfRangeException: Index was outside the bounds of the array.

 

等等

 

去github上EF開源項目,搜索問題發現有人已經提出了一些問題,下面這個應該對應我們上面的字段格式錯誤,因爲EF自動生成的sql查詢字段重複導致了錯位

The column 'X' was specified multiple times for 'Y' #13922

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

 

或者

RowNumberForPaging, two tables and columns with same name #5641

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

 

後來有個大鬍子回覆了,大概意思是問我們爲什麼還在用這個,他們打算棄用的,sql server 2008版本不再打算繼續支持了

Consider removing UseRowNumberForPaging #13959

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

 

Because it is generally only needed in SQL Server 2008, which is out of support.

If you're reading this and you use UseRowNumberForPaging, then please comment on this issue and let us know why you are using it.

 

好吧,掉坑裏去了,不用分頁應該不會出現這個問題,只要使用了sql server 2008的rownumber分頁功能,並且在併發情況下才有概率出現這個問題

坑爹的微軟啊,直接說讓我們用新版本,新版本授權不要錢麼。。。

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章