【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();
            optionsBuilder.UseSqlServer(connStr, b => b.UseRowNumberForPaging());
            builder.RegisterType()
                .As()
                .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();

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

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

                List 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版本不再打算继续支持了

【netcore入坑记】 .Net core UseRowNumberForPaging 分页报错 SQL Server 2008 R2 EntityFrameworkCore..._第1张图片

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年了

等吧,哈哈哈

 

剧终

 

转载于:https://www.cnblogs.com/jhli/p/9962372.html

你可能感兴趣的:(【netcore入坑记】 .Net core UseRowNumberForPaging 分页报错 SQL Server 2008 R2 EntityFrameworkCore...)