DKNet

Read and format a database sequence value

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
}

UseAutoConfigModel registers billing.Seq_Number and NextSeqValueWithFormat obtains a value before formatting it with the declared pattern.

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.

⚠️ Gotchas & limits