Generate sequential GUIDs for MS SQL Server

By Sergey Nosov

December 17, 2021

While using GUIDs or values of the SQL Server UNIQUEIDENTIFIER type for primary keys presents a number of attractive opportunities in design of your software, a couple of hindrances are often quoted when dissuading developers from doing so.

The first issue, is index fragmentation, which is unavoidable with truly random values. And the second, is loss of ability to cohesively order table rows by the primary key.

Both of these deficiencies can be solved by using the OrderFactory.GuidGenerator open-source library which is available as a NuGet package.

This OrderFactory.GuidGenerator library targets .NET standard 2.0, and as such is compatible with variety of .NET frameworks, including .NET Framework 4.6.1 and later, .NET Core 2.0 and later, as well as .NET 5, .NET 6, and so on.

Usage

The simplest usage, after adding the OrderFactory.GuidGenerator NuGet package to your application, is is as follows:

using OrderFactory.GuidGenerator;
var generator = new GuidGenerator();
Console.WriteLine($"Hello, World! {generator.NewGuid()}");

The GuidGenerator class also implements the IGuidGenerator interface which is convenient for dependency injection. We recommend configuring it with singleton lifetime, such as:

builder.Services.AddSingleton<IGuidGenerator, GuidGenerator>();

Copyright © 2012-2021 www.orderfactory.com. All rights reserved.
"Configuring Windows 2008 R2 Web Server" is an independently published book and is not affiliated with, nor has it been authorized, sponsored, or otherwise approved by Microsoft Corporation. Windows is a registered trademark of Microsoft Corporation in the United States and other countries.

Privacy Policy