Minimal integration test showing how to obtain a SQL Server sequence value with EF Core 10 before inserting an entity. The test starts a real SQL Server 2025 container with Testcontainers, creates the sequence, reads its next value, inserts the entity, and verifies the stored row.
The working query deliberately materializes before calling Single():
var values = await db.Database
.SqlQuery<long>($"SELECT NEXT VALUE FOR [Schema].[Custom Value]")
.ToListAsync();
var nextValue = values.Single();Calling SingleAsync() directly is not valid for this SQL. EF Core composes it
as a derived table, but SQL Server does not allow NEXT VALUE FOR in a derived
table. ToListAsync() executes the raw SQL without that composition; Single()
then checks the cardinality in memory.
Prerequisites:
- .NET 10 SDK
- Docker
Run the example:
dotnet test EfNextValue.slnx