cloudscribe / pwakit

aiming to provide tools to facilitate building PWAs (Progressive Web Apps) with ASP.NET Core
Apache License 2.0
10 stars 0 forks source link

Housekeeping: Query splitting and remove stray AddEntityFramework call #46

Open JimKerslake opened 1 year ago

JimKerslake commented 1 year ago

https://learn.microsoft.com/en-us/ef/core/querying/single-split-queries

When using split queries with Skip/Take, pay special attention to making your query ordering fully unique; not doing so could cause incorrect data to be returned. For example, if results are ordered only by date, but there can be multiple results with the same date, then each one of the split queries could each get different results from the database. Ordering by both date and ID (or any other unique property or combination of properties) makes the ordering fully unique and avoids this problem. Note that relational databases do not apply any ordering by default, even on the primary key.

You can also configure split queries as the default for your application's context:

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
    optionsBuilder
        .UseSqlServer(
            @"Server=(localdb)\mssqllocaldb;Database=EFQuerying;Trusted_Connection=True",
            o => o.UseQuerySplittingBehavior(QuerySplittingBehavior.SplitQuery));
}

When split queries are configured as the default, it's still possible to configure specific queries to execute as single queries:

using (var context = new SplitQueriesBloggingContext())
{
    var blogs = context.Blogs
        .Include(blog => blog.Posts)
        .AsSingleQuery()
        .ToList();
}