Wiaoj.Querying.Pagination.EntityFrameworkCore
0.5.1-alpha
dotnet add package Wiaoj.Querying.Pagination.EntityFrameworkCore --version 0.5.1-alpha
NuGet\Install-Package Wiaoj.Querying.Pagination.EntityFrameworkCore -Version 0.5.1-alpha
<PackageReference Include="Wiaoj.Querying.Pagination.EntityFrameworkCore" Version="0.5.1-alpha" />
<PackageVersion Include="Wiaoj.Querying.Pagination.EntityFrameworkCore" Version="0.5.1-alpha" />
<PackageReference Include="Wiaoj.Querying.Pagination.EntityFrameworkCore" />
paket add Wiaoj.Querying.Pagination.EntityFrameworkCore --version 0.5.1-alpha
#r "nuget: Wiaoj.Querying.Pagination.EntityFrameworkCore, 0.5.1-alpha"
#:package Wiaoj.Querying.Pagination.EntityFrameworkCore@0.5.1-alpha
#addin nuget:?package=Wiaoj.Querying.Pagination.EntityFrameworkCore&version=0.5.1-alpha&prerelease
#tool nuget:?package=Wiaoj.Querying.Pagination.EntityFrameworkCore&version=0.5.1-alpha&prerelease
Wiaoj.Querying.Pagination.EntityFrameworkCore
Pages the result of a query contract in a single call, using Entity Framework Core. The call validates the request, applies the filters, search and sort, projects each row to the response type, and pages the result, with offset or keyset pages.
It connects Wiaoj.Querying and Wiaoj.Pagination.EntityFrameworkCore. Because the connection lives in this package, Wiaoj.Querying itself does not depend on EF Core.
Installation
dotnet add package Wiaoj.Querying.Pagination.EntityFrameworkCore
Offset pages
Declare the endpoint's contract as a QuerySchema<TEntity, TResponse> with a tie-breaker:
public sealed class PublicAssetSchema : QuerySchema<Asset, AssetSummaryResponse> {
public PublicAssetSchema() {
Project(a => new AssetSummaryResponse(a.Id.Encode(), a.FileName, a.FileSize));
AllowFilter(a => a.FileName);
AllowSort(a => a.FileSize);
DefaultSort(a => a.FileSize, SortDirection.Descending);
TieBreaker(a => a.Id);
}
}
Then page with one call:
app.MapGet("/assets", async (Query<Asset> query, [AsParameters] PageRequest page, PublicAssetSchema schema, AppDbContext db, CancellationToken ct) =>
TypedResults.Ok(await db.Assets
.Where(a => a.ApplicationId == appId)
.ToPagedResultAsync(query, schema, page, ct)))
.WithQueryValidation<Asset, PublicAssetSchema>()
.WithPagination();
The call does the following:
- Validates the request. A misspelt filter throws
QueryValidationException. It is never silently skipped, which would return a wider result. - Orders the rows. It applies the caller's
sortif there is one, otherwise the schema's default sort, and always ends with the tie-breaker. Without a tie-breaker, rows that share a sort value can appear on two pages and be missing from another, so a schema without one throws. - Projects in SQL. Only the columns the projection uses are read.
- Returns the response shape. The result is
PagedResult<TResponse>with its metadata. There is no mapping step where the metadata could be dropped.
Any ordering you apply to the source before the call is replaced, because the contract decides the order.
Keyset pages
A cursor-paged endpoint lets the caller pick the sort, and the cursor seeks on that sort:
public sealed class AssetFeedSchema : QuerySchema<Asset, AssetSummaryResponse> {
public AssetFeedSchema() {
Project(a => new AssetSummaryResponse(a.Id.Encode(), a.FileName, a.FileSize));
AllowFilter(a => a.FileName);
Property(a => a.CreatedAt).AsCursor(); // sortable, and pageable by cursor
Property(a => a.FileName).AsCursor();
AllowSort(a => a.FileSize); // sortable on offset endpoints only
DefaultSort(a => a.CreatedAt, SortDirection.Descending);
TieBreaker(a => a.Id, id => id.Value.ToString(CultureInfo.InvariantCulture),
text => new AssetId(long.Parse(text, CultureInfo.InvariantCulture)));
}
}
CursorResult<AssetSummaryResponse> page = await db.Assets
.Where(a => a.ApplicationId == appId)
.ToCursorResultAsync(query, schema, cursorRequest, ct);
Why this combination is safe. Applying a client-chosen sort and then paging with a cursor fixed on another key loses rows without any error (#73). Here the cursor is built from the sort keys followed by the tie-breaker, so the seek continues exactly where the ordering stopped.
What the call enforces:
| Situation | Result |
|---|---|
sort= names a field that is not AsCursor() |
QueryValidationException (FieldNotCursorSortable) → 400 |
A cursor is sent back with a different sort, including only a changed direction |
QueryValidationException (CursorSortChanged) → 400 |
| The cursor cannot be read | QueryValidationException (InvalidCursor) → 400 |
No sort= |
The schema's default sort, whose fields must be AsCursor(). With no default sort, the tie-breaker alone. |
| The schema has no tie-breaker, or no codec for it | InvalidOperationException: a server configuration error |
| A reference-typed cursor key is null | Null values sort last in both forward sort directions, with ties ordered by the remaining keys |
Behind WithQueryValidation, a QueryValidationException thrown from the handler becomes the same 400 ValidationProblem that an invalid query string gets.
Cursor keys. Built-in codecs cover strings, integers, decimal, floating point, bool, Guid, DateTime, DateTimeOffset, DateOnly, TimeOnly, TimeSpan and enums. A strongly-typed id needs a codec. A nullable value type cannot be a cursor key.
The cursor itself. Version 2 records null keys distinctly from empty strings. Earlier cursor versions are rejected with InvalidCursor; request the first page without a cursor. A cursor records the sort it was issued for as a fingerprint. That fingerprint detects a changed sort; it is not a signature. If the endpoint should accept only cursors it issued, opt in below.
Opt in to signed keyset cursors
Pass a reusable signing policy explicitly for an endpoint that should accept only cursors it issued. Keep independent endpoint scopes on distinct purpose names, and put the same policy on every replica of a given endpoint. Leave other endpoints unchanged.
using Wiaoj.Pagination.Signing;
// Create once at startup from application secret-store material.
var activeKey = new KeysetCursorSigningKey("2026-12", activeSecretBytes);
var signing = new KeysetCursorSigning("catalog.assets.v1", activeKey);
// The authorization predicate is still required.
CursorResult<AssetSummaryResponse> page = await db.Assets
.Where(a => a.ApplicationId == appId)
.ToCursorResultAsync(query, schema, cursorRequest, signing, ct);
- Format: the signed wire token is still a single base64url string. Its versioned envelope carries a public key ID, the existing v2 keyset bytes (bounded to 8 KiB), and a full HMAC-SHA256 tag. Header and payload are authenticated with the endpoint purpose. This is not the separate timestamped SignedCursorToken format. Applications should treat it as opaque. Bound the encoded sizes of the cursor keys to fit your HTTP infrastructure; page size does not limit the size of an individual cursor. Allow room for the envelope fields and base64url expansion (roughly 4/3 of the binary length).
- Stable pages: there is no timestamp or expiration. The same page, endpoint purpose and active key produce identical cursors, so links and ETags do not change merely with time. Activating another key will change representations that include their cursors.
- Validation: incomplete, unsigned, mis-signed, retired-key or other-purpose cursors return InvalidCursor before any cursor keys are interpreted. A valid signature does not bypass the existing sort fingerprint check.
- Activation: enabling or disabling signing is a deliberate cutover. Previously issued cursors from the other mode do not work. Deploy one mode consistently for an endpoint; tell clients to restart pagination without a cursor. There is no unsigned fallback.
- Rotation: first deploy the new ID and key to every replica as a verification-only key. Next promote it to active everywhere, retaining the old key in verificationKeys. Once the application is ready to retire outstanding cursors for the old ID, remove that key. Never assign different material to an existing key ID. Restart clients from the first page if their old key is retired.
- Security scope: use separate randomly generated keys (at least 32 bytes) from a secret store. The key type copies and retains material in managed memory and does not expose it as a public property. Signing neither encrypts key values nor establishes permission to see rows; always enforce tenant, principal and visibility predicates before paging. A signature does not bind changing filters, enforce freshness or stop replay within its configured purpose.
- Coverage: this opt-in is for the Querying EF keyset adapter only; separate Pagination EF keyset overloads and offset pagination are unchanged. No key or implicit global policy is added to QueryOptions.
Use Wiaoj.Security-managed keys
If the application already uses Wiaoj.Security, prefer deriving a signing key rather than supplying raw key material. Configure your application's existing master-key provider and key store as described in the Security setup, then register a dedicated cursor context and the policy:
services.AddWiaojSecurity()
// Register the application's master-key provider and key store here.
.AddManagedProtector<AssetCursorContext>();
services.AddSingleton(sp => KeysetCursorSigning.FromSecurity(
"catalog.assets.v1",
sp.GetRequiredService<ISubkeyDeriver<AssetCursorContext>>()));
Declare AssetCursorContext as a sealed class implementing ISecretContext. Inject the policy into the endpoint and pass it to the signed paging overload shown above. Keep the context and purpose stable across replicas. No concrete key store is required by the signing package, which only references the Security abstraction. The managed protector must be initialized before serving requests.
The current Security version signs both cursors of a page. A retained older key verifies cursors it issued but is not used for new pages. Removing it makes its outstanding cursors invalid. The signing policy derives a 32-byte purpose-specific subkey only when needed and clears the temporary buffer; it never receives the Security master key. Moving between directly configured keys and Security-derived keys is a key-source cutover: tell clients to restart pagination without a cursor.
For startup-validated appsettings and explicit per-endpoint profile selection, prefer AddKeysetCursorSigning<TProfile, TContext>(section) inside the AddPagination(pagination => ...) callback. The AddQuerying() chain can still forward to the same registration. Inject IKeysetCursorSigning<TProfile> and pass its Policy to signed paging. Use the separate named overload only when selecting a signing policy by name at runtime. See the host composition sample. The fluent Querying overload forwards to Wiaoj.Pagination.Signing. Import Wiaoj.Pagination.Signing for the policy, key and typed service; Querying Core does not reference it.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net11.0 is compatible. |
-
net11.0
- Microsoft.EntityFrameworkCore (>= 11.0.0)
- Microsoft.Extensions.DependencyInjection (>= 11.0.0)
- Microsoft.Extensions.Logging (>= 11.0.0)
- Wiaoj.Pagination.EntityFrameworkCore (>= 0.5.1-alpha)
- Wiaoj.Pagination.Signing (>= 0.5.1-alpha)
- Wiaoj.Preconditions (>= 0.5.1-alpha)
- Wiaoj.Primitives (>= 0.5.1-alpha)
- Wiaoj.Querying (>= 0.5.1-alpha)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 0.5.1-alpha | 0 | 12/6/2026 |
| 0.5.0-alpha.1 | 0 | 12/5/2026 |
| 0.4.2-alpha.2 | 52 | 12/3/2026 |
| 0.4.2-alpha.1 | 50 | 12/2/2026 |
| 0.4.1-alpha.3 | 61 | 11/30/2026 |
| 0.4.1-alpha.2 | 63 | 11/29/2026 |
| 0.4.1-alpha.1 | 59 | 11/28/2026 |
| 0.4.0-alpha.5 | 69 | 11/27/2026 |
| 0.4.0-alpha.4 | 59 | 11/15/2026 |
| 0.4.0-alpha.3 | 67 | 11/4/2026 |
| 0.4.0-alpha.1 | 67 | 10/30/2026 |
| 0.3.0-alpha.1 | 2,392 | 10/5/2026 |
| 0.2.0-alpha.3 | 76 | 9/24/2026 |
| 0.2.0-alpha.2 | 67 | 9/24/2026 |
| 0.2.0-alpha.1 | 73 | 9/24/2026 |
| 0.1.0-alpha.9 | 75 | 9/21/2026 |
| 0.1.0-alpha.8 | 62 | 9/21/2026 |
| 0.1.0-alpha.7 | 81 | 9/18/2026 |
| 0.1.0-alpha.6 | 68 | 9/16/2026 |
| 0.1.0-alpha.5 | 71 | 9/16/2026 |