UseHiLo() makes EF Core get keys from a database sequence (EntityFrameworkHiLoSequence by default, INCREMENT BY 10). One round trip reserves a block of values; EF Core then hands out ids from that block in memory before SaveChanges, so entities have real keys early and inserts batch without an OUTPUT of identity values. The database still guarantees uniqueness, because every instance reserves its own block from the same sequence. Trade-offs: gaps (a restarted process or a recycled context loses the rest of its block), ids are not in insert order across instances, the sequence becomes shared schema every writer must use (raw SQL inserts must call NEXT VALUE FOR), and a block too small means more sequence calls while a block too large means bigger gaps. Size the block to insert throughput: UseHiLo("OrderSeq") with the sequence's increment set, for example, to 100 for bulk imports.
"HiLo is a client-side key allocation scheme backed by the database. EF Core asks a SQL Server sequence for the next value; with the default increment of 10 that value reserves ten ids for this process. It then assigns ids from that block in memory, and only goes back to the sequence when the block is used up. Every instance does the same, and because the sequence hands out each block once, ids stay unique across all of them.
The benefit is that an entity has its key as soon as it is added, so I can wire up references or return the id before saving, and inserts batch cleanly because EF Core does not need identity values back from each row. For a large import it cuts round trips.
The costs: gaps, because a process that restarts throws away the rest of its block; ids that are not in creation order across instances, so they are no good for sorting by time; and the sequence becomes part of the contract, so a stored procedure or
SqlBulkCopymust callNEXT VALUE FORtoo. I size the increment to throughput, for example 100 for an import service, accepting bigger gaps. Today, for many systemsGuid.CreateVersion7()gives the same 'key before save' benefit without the shared sequence, so I treat HiLo as a choice for integer keys specifically."
| Identity | HiLo | GUID v7 | |
|---|---|---|---|
| Key before SaveChanges | No | Yes | Yes |
| Round trips for keys | Read back per batch | One per block | None |
| Gaps | On rollback | Per unused block | n/a |
| Ordered across instances | Yes | Roughly per block | Roughly by time |
| Size | 4/8 bytes | 4/8 bytes | 16 bytes |
modelBuilder.HasSequence<long>("OrderSeq").IncrementsBy(100);
modelBuilder.Entity<Order>().Property(o => o.Id).UseHiLo("OrderSeq");
An import of [2 million] rows with HiLo at increment 100 needed [~20,000] sequence calls instead of reading back identities per batch, and cut import time by [~30%]; a nightly restart left gaps that nobody cared about once the team agreed ids carry no meaning.
Checks understanding of client-side key generation.
Last updated 23 Nov 2026.