【netcore入坑記】 .Net core 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分頁功能,並且在並發情況下才有概率出現這個問題

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

 

更新於2018-11-27

后來大胡子又說不打算停止更新了,將繼續支持這個版本的分頁

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

 

不過將在3.0版本進行發布,估計得2019年了

等吧,哈哈哈

 

劇終

 


免責聲明!

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



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