Part of DKNet.EfCore.Extensions. Use the enum sequence metadata to register a model sequence and fetch its next value on SQL Server or Npgsql.
Create the Quick Start console project and add Npgsql.EntityFrameworkCore.PostgreSQL 10.x. Set DKNET_DOCS_DB to a connection string for an empty PostgreSQL database whose role can create schemas, tables and sequences. This example creates the model with EnsureCreatedAsync; run it in its own database. Replace Program.cs with:
using Microsoft.EntityFrameworkCore;
var connectionString = Environment.GetEnvironmentVariable("DKNET_DOCS_DB")
?? throw new InvalidOperationException("Set DKNET_DOCS_DB to an empty PostgreSQL database.");
var o = new DbContextOptionsBuilder<DemoDb>();
o.UseNpgsql(connectionString).UseAutoConfigModel<DemoDb>();
await using var db = new DemoDb(o.Options);
await db.Database.EnsureCreatedAsync();
Console.WriteLine(await db.NextSeqValueWithFormat(InvoiceSequences.Number));
// INV-001000 on the first read in a fresh database
public sealed class DemoDb(DbContextOptions<DemoDb> options) : DbContext(options)
{
public DbSet<Item> Items => Set<Item>();
}
public sealed class Item
{
public int Id { get; set; }
public string Name { get; set; } = "";
}
[DKNet.EfCore.Abstractions.Attributes.SqlSequence("billing")]
public enum InvoiceSequences
{
[DKNet.EfCore.Abstractions.Attributes.Sequence(typeof(long), StartAt = 1000, FormatString = "INV-{1:000000}")]
Number
}
NextSeqValue<TEnum> returns the raw scalar; the two-generic-argument overload casts it to TValue?. NextSeqValueWithFormat returns text. EF-managed open/close calls are balanced in a finally block even when execution fails.
[SqlSequence] returns null from the raw reader. A member without [Sequence] is not registered, although a read can still attempt its name.NotSupportedException when a marked enum is read. Registration silently skips sequences outside SQL Server/Npgsql.{1} is the number. DateTime is replaced with 0 in the format and bound to UTC now; formatting uses invariant culture.