使用 Entity Framework 7 和 SQL Server 2008 进行分页 [英] Paging with Entity Framework 7 and SQL Server 2008

查看:37
本文介绍了使用 Entity Framework 7 和 SQL Server 2008 进行分页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在实体框架 7 中使用分页(即 .Skip(...).Take(...).它适用于 Microsoft SQL Server 2012 和 2014,但在 SQL Server 2008 上失败并出现以下错误:

I'm trying to use paging (that is .Skip(...).Take(...) in Entity Framework 7. It works OK with Microsoft SQL Server 2012 and 2014, but fails with the following error on SQL Server 2008:

System.Data.SqlClient.SqlException (0x80131904):'OFFSET' 附近的语法不正确.FETCH 语句中选项 NEXT 的使用无效.

System.Data.SqlClient.SqlException (0x80131904): Incorrect syntax near 'OFFSET'. Invalid usage of the option NEXT in the FETCH statement.

我发现这是 EF 6.1.2 版的重大更改(http://erikej.blogspot.com/2014/12/a-breaking-change-in-entity-framework.html).但修复是修改 EDMX 文件设置 ProviderManifestToken 属性为2008".

I've figured out that it is a breaking change in EF version 6.1.2 (http://erikej.blogspot.com/2014/12/a-breaking-change-in-entity-framework.html). But the fix is to modify EDMX file setting ProviderManifestToken attribute to "2008".

问题是EF7目前只支持代码优先方案,因此没有任何EDMX.问题是:如何使用 Entity Framework 7 配置 ASP.NET 5 网站以对 2012 年以前的 SQL Server 使用回退分页方法?

The problem is that EF7 currently only supports code-first scenario, thus there is no any EDMX out there. The question is: how to configure ASP.NET 5 website with Entity Framework 7 to use fallback pagination approach for SQL Server older than 2012?

推荐答案

我自己在使用 EF 7 和 sql server 2008 时遇到了这个问题.幸运的是,在 EF 7 的最新 rc1 版本中,您可以使用 .UseRowNumberForPaging() 作为解决这个问题在这个例子中显示:

I encountered this problem myself using EF 7 and sql server 2008. Fortunately in the latest rc1 version of EF 7 you can solve this by using .UseRowNumberForPaging() as shown in this example:

services.AddEntityFramework()
  .AddSqlServer()
  .AddDbContext<YourDbContext>(options =>
     options.UseSqlServer(configuration["Data:DefaultConnection:ConnectionString"])
                    // this is needed unless you are on mssql 2012 or higher
                    .UseRowNumberForPaging()
                );

这篇关于使用 Entity Framework 7 和 SQL Server 2008 进行分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆