liveBook · Manning

Identity column

As explained in Building Web APIs with ASP.NET Core

An identity column is a SQL Server table column whose values are automatically generated, typically by incrementing a stored counter.

An identity column is a SQL Server column whose values the database generates and increments automatically. In the BoardGames table, EF Core assigns this behavior to the Id primary-key column. section 5.3.6

When IDENTITY_INSERT is OFF, inserting an explicit Id value fails. Seeding CSV records therefore requires either omitting Id and accepting new values or temporarily allowing explicit identity values. section 5.3.6

Preserving imported IDs during seeding

Wrap the save operation in a transaction, set IDENTITY_INSERT BoardGames ON, call SaveChangesAsync, turn the setting OFF, and commit. This preserves the CSV identifiers while allowing SQL Server to accept them. section 5.3.6

How Building Web APIs with ASP.NET Core covers it

How deep it goes
Explains automatic assignment, the explicit-value failure, and two fixes, then implements the preserving-ID option.
Examples
Uses a failed PUT /Seed request, a SQL exception, CSV identifiers, and a transaction-wrapped SaveChangesAsync sequence.
What you can do after
Diagnose EF Core seeding failures and preserve imported identifiers by configuring IDENTITY_INSERT around the save operation.
How it connects
Connects identity columns with SQL Server, EF Core, primary keys, migrations, transactions, and IDENTITY_INSERT.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Building Web APIs with ASP.NET Core. The section links point to the parts of the book it draws on.