In EF, it is convenient to use LINQ for paging, if we have an EMP table with the following structure:
public class Emp { [Key] public Guid No {get; set;} public int Age {get; set;} [Required] [Stringlength ()] public string Name {get; set;} }
If we are paging, we generally use the skip and take methods, where the simplest line of code is as follows:
MContext.Emp.OrderBy (emp = EMP). Name). Skip (6). Take (3);
If we are using EF6, when using SQL Server R2 and earlier databases, the following SQL statements are generated automatically:
SELECT TOP (3) [extent1].[ No] as [no], [extent1].[ Age] as [age], [extent1].[ Name] as [name] from (SELECT [extent1].[ No] as [no], [Extent1]. [Age] As [age], [Extent1]. [Name] As [Name], Row_number () over (ORDER by [extent1].[ Name] ASC) as [row_number] from [dbo].[ EMP] as [Extent1] ) as [Extent1] WHERE [extent1].[ Row_number] > 6 ORDER by [extent1].[ Name] ASC
If we are using EF6, when using SQL Server 2012 or 2014 databases, the following SQL statements are generated automatically:
SELECT [extent1].[ No] as [no], [extent1].[ Age] as [age], [extent1].[ Name] as [name] from [dbo].[ EMP] as [Extent1] ORDER by [extent1].[ Name] ASC
Because in SQL server 2012 and later databases, there is a higher performance paging method that uses offset ROWS FETCH NEXT rows only keywords, EF automatically generates matching SQL statements based on different versions of the database.
However, in Ef7beta7, perhaps the problem of a bug, always follow the method of fetch NEXT to generate, specifically as follows:
Using EF7BETA7, when using SQL Server R2 and earlier databases, the SQL statements are generated automatically as follows:
Select "EMP". " No "," emp "." Age "," emp "." Name "from " emp "as" EMP " ORDER by" EMP "." Name " OFFSET 6 rows FETCH NEXT 3 rows only
Using EF7BETA7, when using a SQL Server 2012 or 2014 database, the following SQL statements are generated automatically:
Select "EMP". " No "," emp "." Age "," emp "." Name "from " emp "as" EMP " ORDER by" EMP "." Name " OFFSET 6 rows FETCH NEXT 3 rows only
That is, the SQL statements generated by the two are the same, and there is a problem, and if you use LINQ for paging, you will get an error when using SQL Server R2 and earlier databases.
Briefly, this error should be generated in the EF7 source code in the Sqlserverquerysqlgenerator class and its affiliated classes, and hopefully the new version will solve the problem.
Entity Framewrok 7beta7 in different versions of SQL Server Automatic Component page SQL statement issues