Search code examples
c#asp.netsql-server-2008asp.net-coreentity-framework-core

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


Here's my code:

 var result = dbContext.Specialty.OrderByDescending(u => u.IdS)
            .Skip(20)
            .Take(10)
            .AsEnumerable();

Error:

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


Solution

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