Every version: AllVersions
Show me this policy's whole history.
cs
var timeline = await db.Policies
.AllVersions()
.Where(p => p.Id == policyId)
.ToListAsync(); // newest firstAllVersions() reads from the history table and returns every stored version: one per insert and one per update. There is no period filter; this is the full timeline, not a point in time.
- Rows come back newest first, by
valid_fromdescending. Your ownOrderByorOrderByDescendingreplaces that order; a trailingThenBykeeps newest first as the primary order. - Deletes are not versions. A deleted policy's timeline ends at the version that was current when it was deleted. To see the delete itself, when and by whom, use
History<T>.
Composing
Like AsOf, it must be the first operator on the query, and everything after it composes into one SQL statement:
cs
var premiumsEverCharged = await db.Policies
.AllVersions()
.Where(p => p.Id == policyId)
.Select(p => p.Premium)
.Distinct()
.ToListAsync();The generated SQL
For a filter on Status (from Hindsight's test suite). operation <> 3 excludes tombstones:
sql
SELECT p.id, p.metadata, p.number, p.premium, p.status, p.tags
FROM policies_history AS p
WHERE p.operation <> 3 AND p.status = 'Active'
ORDER BY p.valid_from DESCAllVersions returns entity snapshots only. When you also need each version's period or change context, use History<T>.