Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Incorrect syntax near 'OFFSET'. Invalid usage of the option NEXT in the FETCH statement "in Entity Framework core"

Here's my EF Core code:

 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();

Error:

Incorrect syntax near 'OFFSET'. Invalid usage of the option NEXT in the FETCH statement

like image 655
Alireza Avatar asked Nov 17 '18 07:11

Alireza


4 Answers

There is a compatibility setting (UseRowNumberForPaging) for this which can be configured either in the DbContext itself:

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        var coonectionString = "Data Source=localhost\\MSSQLSERVER01;Initial Catalog=AppDb01;Integrated Security=True";
        optionsBuilder.UseSqlServer(coonectionString, builder => builder.UseRowNumberForPaging());
    }

Or as a part of the Startup:

    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()));
    }
like image 100
Elliott Avatar answered Oct 24 '22 10:10

Elliott


UseRowNumberForPaging was removed in EF Core 3.x, method is marked as obsolete. However, you can use EfCore3.SqlServer2008Query package instead. There are 2 packages available in Nuget, one for >= .NET 5.0, other one is for >= .NET Core 3.1

enter image description here

Usage:

 services.AddDbContext<MyDbContext>(o => 
       o.UseSqlServer(Configuration.GetConnectionString("Default"))
        .ReplaceService<IQueryTranslationPostprocessorFactory, SqlServer2008QueryTranslationPostprocessorFactory>());
like image 40
Yegor Androsov Avatar answered Oct 24 '22 09:10

Yegor Androsov


For anyone using .Net 6 I found this package EntityFrameworkCore.UseRowNumberForPaging 0.3.0: https://www.nuget.org/packages/EntityFrameworkCore.UseRowNumberForPaging/

Base on the github issue: https://github.com/dotnet/efcore/issues/16400

Thank you Rwing, whoever you are, you're a hero!

like image 32
Jawid Hassim Avatar answered Oct 24 '22 08:10

Jawid Hassim


sql server 2008 not support from my query

solution:

public class AppDbContext : DbContext
{

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        var coonectionString = "Data Source=localhost\\MSSQLSERVER01;Initial Catalog=AppDb01;Integrated Security=True";
        optionsBuilder.UseSqlServer(coonectionString);
    }
}

Value connection string to the Target server and also inject the settings , The sample code is in the default ASP NET Core project format.

like image 1
Alireza Avatar answered Oct 24 '22 08:10

Alireza