DEV Community

Cover image for EF Core UseIdentityColumns
Karen Payne
Karen Payne

Posted on

EF Core UseIdentityColumns

Introduction

UseIdentityColumns is useful to set a custom seed and increment value, such as starting IDs at 1000 instead of 1 or starting at 1 and configuring the increment, for example, starting at 1 and incrementing by 10.

💡 Only for SQL Server

Product Versions
Entity Framework Core 6.0, 7.0, 8.0, 9.0, 10.0

Used

  • Microsoft Visual Studio 2026
  • NET 10 Framework
  • EF Core 10.0.8

Model for demonstrations

public partial class Customer
{
    public int CustomerId { get; set; }

    public string FirstName { get; set; }

    public string LastName { get; set; }
}
Enter fullscreen mode Exit fullscreen mode

Start primary key at 100, increment by 1

A business rule requires the Customer CustomerId to start at 100 and increment by 1.

Pass 100 into UseIdentityColumns.

Shows seeding start at 100 rather than 1

Start primary key at 10, increment by 10

A business rule requires the Customer CustomerId to start at 10 and increment by 10.

shows seeding starts at 10 and increments by 10

Sample code

Source code

Has been set up to create the database each time the console project runs to try out different configurations for UseIdentityColumns.

The connection string is in appsettings.json

{
  "ConnectionStrings": {
    "MainConnection": "Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=EF_IdentityOptions;Integrated Security=True;Encrypt=False"
  }
}
Enter fullscreen mode Exit fullscreen mode
  • Classes\DataOperations.cs has all EF Core operations.
  • SpectreConsoleLibrary is included for custom console output

Top comments (0)