Entity Framework Core is the primary ORM for .NET applications. UUID v7 integrates through custom value generators or explicit assignment, giving EF Core models time-ordered primary keys without a separate timestamp column. .NET 9 introduced Guid.CreateVersion7() as a built-in API. See DotNetConf 2026 for the announcement context.

Install packages

dotnet add package Microsoft.EntityFrameworkCore
dotnet add package Npgsql.EntityFrameworkCore.PostgreSQL   # PostgreSQL
# or
dotnet add package Microsoft.EntityFrameworkCore.SqlServer # SQL Server

UUID v7 value generator (.NET 9+)

using Microsoft.EntityFrameworkCore.ChangeTracking;
using Microsoft.EntityFrameworkCore.ValueGeneration;

public class UuidV7ValueGenerator : ValueGenerator<Guid>
{
    public override Guid Next(EntityEntry entry)
        => Guid.CreateVersion7(); // .NET 9+

    public override bool GeneratesTemporaryValues => false;
}

Entity and DbContext

public class Order
{
    public Guid   Id         { get; set; }
    public Guid   CustomerId { get; set; }
    public string Status     { get; set; } = "pending";
    public decimal Total     { get; set; }
}

public class AppDbContext : DbContext
{
    public DbSet<Order> Orders { get; set; } = null!;

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Order>(entity =>
        {
            entity.HasKey(e => e.Id);

            // Register UUID v7 value generator
            entity.Property(e => e.Id)
                  .HasValueGenerator<UuidV7ValueGenerator>();

            // PostgreSQL: native uuid column (16 bytes)
            // SQL Server: uniqueidentifier column (16 bytes)
        });
    }

    protected override void OnConfiguring(DbContextOptionsBuilder options)
        => options.UseNpgsql(Environment.GetEnvironmentVariable("DATABASE_URL"));
}

Insert — value generator assigns UUID v7 automatically

using var db = new AppDbContext();

var order = new Order
{
    // Id is assigned by UuidV7ValueGenerator before INSERT
    CustomerId = Guid.Parse("018fbe3a-4c5d-7000-8000-000000000001"),
    Total      = 49.99m,
};

db.Orders.Add(order);
await db.SaveChangesAsync();

Console.WriteLine($"Created: {order.Id}"); // UUID v7

Cursor pagination

async Task<(List<Order> Data, Guid? NextCursor)> ListOrdersAsync(
    AppDbContext db,
    Guid customerId,
    Guid? cursor,
    int limit = 20)
{
    var query = db.Orders
        .Where(o => o.CustomerId == customerId);

    if (cursor.HasValue)
    {
        // UUID v7 Guid comparison is lexicographic = chronological
        query = query.Where(o => o.Id.CompareTo(cursor.Value) > 0);
    }

    var rows = await query
        .OrderBy(o => o.Id) // UUID v7 sort = chronological
        .Take(limit + 1)
        .ToListAsync();

    var hasMore   = rows.Count > limit;
    var data      = hasMore ? rows.Take(limit).ToList() : rows;
    var nextCursor = hasMore ? data.Last().Id : (Guid?)null;

    return (data, nextCursor);
}

Extract creation timestamp from UUID v7

using System;

public static class UuidV7Extensions
{
    // Extract Unix milliseconds from the top 48 bits of a UUID v7 Guid
    public static DateTimeOffset ExtractTimestamp(this Guid guid)
    {
        var bytes = guid.ToByteArray();
        // .NET Guid byte layout differs from RFC 9562 byte order for Guid
        // Use string parsing for portable extraction
        var hex = guid.ToString("N").Substring(0, 12); // top 48 bits
        var ms  = Convert.ToInt64(hex, 16);
        return DateTimeOffset.FromUnixTimeMilliseconds(ms);
    }
}

// Usage
var createdAt = order.Id.ExtractTimestamp();
Console.WriteLine($"Order created: {createdAt:O}");

Migration

EF Core migrations work unchanged — UUID v7 is just a Guid from EF’s perspective:

// Generated migration (PostgreSQL)
protected override void Up(MigrationBuilder migrationBuilder)
{
    migrationBuilder.CreateTable(
        name: "Orders",
        columns: table => new
        {
            Id = table.Column<Guid>(type: "uuid", nullable: false),
            // ...
        },
        constraints: table =>
        {
            table.PrimaryKey("PK_Orders", x => x.Id);
        });
}

No migration changes are needed when switching from UUID v4 to UUID v7 — both are uuid columns in PostgreSQL.

SQL Server uniqueidentifier note

SQL Server stores uniqueidentifier (GUID) in a non-standard byte order that affects index sort order. For sequential inserts, use newsequentialid() as the database default or ensure UUIDs are generated with the correct byte layout. The UUIDNext library handles this for SQL Server compatibility.

Further reading

External references

Frequently asked questions

How do I use Guid.CreateVersion7() with Entity Framework Core?

Assign Guid.CreateVersion7() in a custom value generator: implement ValueGenerator<Guid> and override Next() to return Guid.CreateVersion7(). Register it in OnModelCreating with entity.Property(e => e.Id).HasValueGenerator<UuidV7ValueGenerator>(). Available in .NET 9+. See DotNetConf 2026 — Guid v7.

Does EF Core with PostgreSQL store UUID v7 efficiently?

Npgsql maps Guid to PostgreSQL uuid — 16 bytes stored as a native UUID type. UUID v7 sorts chronologically in PostgreSQL's B-tree index, so ORDER BY id is efficient without a secondary created_at index. Npgsql does not reorder UUID bytes unlike older SQL Server drivers. See UUID in Databases for storage details.

How do I implement cursor pagination with UUID v7 in EF Core?

Query Where(e => e.Id.CompareTo(cursor) > 0).OrderBy(e => e.Id). Guid.CompareTo() does a lexicographic byte comparison which equals chronological for UUID v7. In SQL, this translates to WHERE id > @cursor ORDER BY id — the same pattern as raw SQL cursor pagination. See UUID v7 for Cursor Pagination.