Я использую следующий код для пагинации в EF Core:
int page = 1, rowPerPage = 5;
int count = ctx.Specialty.Count();
int start = page * rowPerPage;
var Select = ctx.Specialty.OrderByDescending(u => u.IdS)
.Skip(start)
.Take(rowPerPage)
.AsEnumerable();
Использую SQL Server 2008 и Visual Studio 2017, проект ASP.NET Core.
Получаю следующиую ошибку:
Incorrect syntax near 'OFFSET'. Invalid usage of the option NEXT in the FETCH statement
Как исправить данную проблему?
Свободный перевод вопроса Incorrect syntax near 'OFFSET'. Invalid usage of the option NEXT in the FETCH statement "in Entity Framework core" от участника @Alireza.
Существует параметр совместимости (UseRowNumberForPaging) для подобного случая, может быть сконфигурировать либо в DbContext:
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
var coonectionString = "Data Source=localhost\\MSSQLSERVER01;Initial Catalog=AppDb01;Integrated Security=True";
optionsBuilder.UseSqlServer(coonectionString, builder => builder.UseRowNumberForPaging());
}
Либо в Startup.cs:
public void ConfigureServices(IServiceCollection services)
{
var coonectionString = "Data Source=localhost\\MSSQLSERVER01;Initial Catalog=AppDb01;Integrated Security=True";
services.AddDbContext<AppDbContext>(options => options.UseSqlServer(coonectionString, builder => builder.UseRowNumberForPaging()));
}
Свободный перевод ответа https://stackoverflow.com/a/54200998/5752652 от участника @Elliott.
Сборка персонального компьютера от Artline: умный выбор для современных пользователей