.NET’s System.Guid struct has represented 128-bit identifiers since .NET 1.0. With .NET 9, Guid.CreateVersion7() brings RFC 9562 UUID v7 into the standard library — no NuGet packages required for new projects.

Generating GUIDs/UUIDs in .NET

.NET 9+ — standard library:

// UUID v4 (random)
Guid v4 = Guid.NewGuid();

// UUID v7 (time-ordered, .NET 9+)
Guid v7 = Guid.CreateVersion7();

Console.WriteLine(v7); // 018fbe3a-4c5d-7000-8abc-0123456789ab

.NET 8 and earlier — UUIDNext package:

dotnet add package UUIDNext
using UUIDNext;

Guid v7 = Uuid.NewSequential(); // UUID v7

Entity Framework Core

EF Core maps Guid properties to uniqueidentifier in SQL Server and uuid in PostgreSQL automatically.

Configuring UUID v7 as the default key generator:

public class Order
{
    public Guid Id { get; set; } = Guid.CreateVersion7(); // .NET 9+
    public string CustomerId { get; set; } = "";
}

// In DbContext:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Entity<Order>()
        .Property(o => o.Id)
        .HasDefaultValueSql("gen_random_uuid()"); // PostgreSQL 18: uuidv7()
}

For application-layer generation (recommended for portability), set the default in the C# class itself rather than via HasDefaultValueSql.

SQL Server Byte-Order Caveat

SQL Server’s uniqueidentifier type stores GUIDs in a non-standard byte layout: groups 1–3 are stored in little-endian byte order, while groups 4–5 are big-endian. This does not affect equality checks but breaks chronological sort order for UUID v7 values in clustered indexes — the index will not be sequential even though the GUID was generated sequentially.

Options for SQL Server with sequential GUIDs:

  1. Store as binary(16) (big-endian bytes) — correct sort order, requires manual conversion in queries
  2. Use NEWSEQUENTIALID() — sequential for the session but resets on server restart
  3. Use EF Core’s SequentialGuidValueGenerator — generates GUIDs sequential in SQL Server’s sort order

For PostgreSQL and MySQL backends, Guid.CreateVersion7() works correctly with no workaround needed.

PostgreSQL with Npgsql

The Npgsql EF Core provider maps Guid to PostgreSQL’s native uuid column type. UUID v7 values insert in ascending B-tree order, giving the same index performance as sequential integers.

// Program.cs / DI setup
builder.Services.AddDbContext<AppDbContext>(opt =>
    opt.UseNpgsql("Host=localhost;Database=mydb;Username=myuser;Password=mypassword"));

No additional configuration is needed for UUID v7 when using PostgreSQL.

Frequently asked questions

How do I generate UUID v7 in .NET and C#?

.NET 9+ includes Guid.CreateVersion7() in the standard library. For .NET 8 and earlier, use the UUIDNext NuGet package. Note: SQL Server's uniqueidentifier type sorts GUIDs in a non-standard byte order — use binary(16) or PostgreSQL for correct chronological index ordering with UUID v7.

Do UUIDs hurt database index performance?

UUID v4 does — its randomness causes every insert to land at a different leaf page, leading to page splits and poor cache locality. UUID v7 embeds a millisecond timestamp so consecutive inserts cluster together, behaving like an auto-increment integer for B-tree purposes.

Does PostgreSQL 18 support UUID v7 natively?

Yes. PostgreSQL 18 ships a built-in uuidv7() function that generates RFC 9562-compliant UUID v7 values directly in SQL — no extensions or application-level generation required. The values store in the existing uuid column type unchanged.

Does .NET support UUID v7 natively?

Yes. .NET 9 (released November 2024) added Guid.CreateVersion7() to the System.Guid struct. It generates an RFC 9562-compliant UUID v7 with a millisecond Unix timestamp prefix. See the Microsoft documentation for Guid.CreateVersion7. For .NET 8 and earlier, use the UUIDNext NuGet package.

Why does SQL Server sort GUIDs differently?

SQL Server's uniqueidentifier type sorts by a non-standard byte group order — it compares groups 10–15 first, then 8–9, then 7, then 6, then 0–4. This means even a time-ordered UUID v7 will not sort chronologically in a SQL Server clustered index. Use NEWSEQUENTIALID() or the Guid.CreateVersion7() value stored as binary(16) for correct ordering in SQL Server.