Paging with Entity Framework 7 and SQL Server 2008

后端 未结 6 448
我寻月下人不归
我寻月下人不归 2020-12-08 10:35

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

相关标签:
6条回答
  • 2020-12-08 11:10

    It's broken in RC 1. Gotta wait to get RC 2.

    https://github.com/aspnet/EntityFramework/issues/4616

    0 讨论(0)
  • 2020-12-08 11:11

    If you use Edmx file, you must open the edmx file using XML Editor and change

    ProviderManifestToken="2012" ==> ProviderManifestToken="2008"
    

    in line 7.

    Please take a look at this blog post for more information: http://erikej.blogspot.com.tr/2014/12/a-breaking-change-in-entity-framework.html

    0 讨论(0)
  • 2020-12-08 11:16

    Here, just set UseRowNumberForPaging() in ConfigureServices

    services.AddDbContext<CallcContext>(options => 
    options.UseSqlServer(Configuration.GetConnectionString("Connectionstring"),opt=> { opt.UseRowNumberForPaging(); }));
    
    0 讨论(0)
  • 2020-12-08 11:23

    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()
                    );
    
    0 讨论(0)
  • 2020-12-08 11:26

    MyDbConnectionString is Connection string from any source

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseSqlServer(_config["MyDbConnectionString"], 
                    options=>
                    {
                        options.UseRowNumberForPaging();
                    });
    }
    

    UseRowNumberForPaging() solved issue in any case except for edmx file scenario.

    0 讨论(0)
  • 2020-12-08 11:33

    You need to use something like this :

    var MinPageRank = (pageIndex - 1) * pageSize + 1;
    var MaxPageRank = (pageIndex * pageSize);
    
    var person = _context.Person.FromSql($"SELECT * FROM (SELECT [RANK] = ROW_NUMBER() OVER (ORDER BY Surname),* FROM Person) A WHERE A.[RANK] BETWEEN {MinPageRank} AND {MaxPageRank}").ToList();
    
    IQueryable<Person> PersonIQ = from s in person.AsQueryable() select s;
    Person = await PaginatedList<Person>.CreateAsync(PersonIQ .AsNoTracking(), pageIndex ?? 1, pageSize, sourceFull);
    
    0 讨论(0)
提交回复
热议问题