DriversRecommendedOutdated drivers can make a good PC feel brokenScan driver issues before chasing fixes manually.Scan NowHalloween DealsAmazon USHalloween week deals: useful picks before month-endAmazon US: current deals, useful picks and tech finds.Check DealsClean PCRecommendedOne scan can reveal what keeps slowing WindowsLook for cleanup and repair opportunities.Run Scan×
Skip to content
World desk6 min

The Real Cost of Returning the Identity Value in EF Core

In the documented EF Core 7 SQL Server insert, the identity comes back through OUTPUT INSERTED, not a separate query. Here is what that means for real save latency.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

In the simple SQL Server case that Microsoft documented for EF Core 7, EF Core does not run a separate query to read back an identity value. The insert itself returns the generated key through an OUTPUT INSERTED clause, so the key comes back in the same statement that wrote the row. What that costs in your application has no universal figure. It depends on how many commands your SaveChanges call sends, how far the application sits from the database, what the table contains, and which EF Core and provider versions you run.

What happens when SaveChanges inserts a row with a generated key

EF Core’s default conventions set most non-composite primary keys of type short, int, long, or Guid up for value generation when an entity is added and no key is supplied. The provider decides how that generation actually works. For SQL Server, numeric properties configured as generated on add are conventionally mapped to IDENTITY columns (Microsoft Learn, “SQL Server Value Generation – EF Core” and “Generated Values – EF Core”).

As an Amazon Associate I earn from qualifying purchases.

Before the row exists in the database, EF Core can hand a new tracked entity a temporary key value. This lets the change tracker link a new principal to its new dependents while the graph is still unsaved. During SaveChanges, EF Core writes the row, reads the real database-generated value back, and updates the tracked entity. If dependent entities reference that principal, EF Core propagates the generated key to them as well (Microsoft Learn, “Explicitly Tracking Entities – EF Core” and “Saving Data – EF Core”).

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

How the SQL Server provider returns an IDENTITY value

The question of “an extra round trip” depends on the statement shape. In the EF Core 7 release notes (Microsoft Learn, “What’s New in EF Core 7.0”), the documented simple SQL Server identity insert is emitted with an OUTPUT INSERTED clause that names the key column. The shape is roughly the following; the exact text, parameter names, and formatting vary by version, so treat this as an illustration rather than a copy of the generated SQL:

INSERT INTO [Blogs] ([Url])
OUTPUT INSERTED.[Id]
VALUES (@p0);

Because the database returns the new Id as part of the insert’s result set, the provider has no need to issue a second statement to ask for it. The sample execution logged on that release page is shown at 25 ms. That figure describes one example execution in that documentation, not a typical latency, and it cannot be used to price identity retrieval on your server.

This is a version-specific description. Later EF Core and provider releases, other table shapes, and other insert patterns may produce different SQL, so check the generated statements in your own build rather than assuming the EF Core 7 example.

Why there is no fixed latency penalty

The key value is one small piece of a save. Microsoft’s guidance does not give an isolated number for the cost of returning one identity, and the factors below are what move the observed time of a SaveChanges call.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Network round trips

Each command the application sends costs a network exchange. On a database on the same machine or the same fast network, a round trip may be small. Across a link with real latency, it can dominate. EF Core 7’s release notes attribute many of its SaveChanges improvements to fewer round trips and faster SQL, which shows that round-trip count is a primary cost driver for the whole save path.

Batching

SaveChanges batches multiple changes in many cases, so several inserts, updates, and deletes can share round trips. Microsoft’s batching guidance (Microsoft Learn, “Efficient Updating – EF Core” and “Saving Data – EF Core”) cites a default batch size of 42 statements for SQL Server and notes that the benefit of batching levels off at around 40 statements. That guidance describes batching in general; it does not measure what returning a single identity adds to a batch.

Transactions

Most single SQL statements already execute transactionally, so EF Core usually does not need an explicit transaction around a single statement. Adding a transaction where the save would not otherwise need one can add round trips (Microsoft Learn, “Transactions – EF Core”). If your code wraps every save in its own explicit transaction, measure that wrapper separately from the key return.

Dependent inserts

When child rows need the parent’s generated key, the application depends on that key being available before the children are written. EF Core manages retrieval and propagation, so you do not write that plumbing yourself. The cost shows up in the number of commands and in ordering constraints, not in a separate identity query you can point to.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

SQL Server table features

The optimized OUTPUT path has limits. Microsoft’s SQL Server provider documentation (Microsoft Learn, “Miscellaneous – Microsoft SQL Server Database Provider – EF Core”) describes a trigger limitation, and the SQL Server provider exposes UseSqlOutputClause to turn the clause off for a table. When the clause is disabled, EF Core uses a different write pattern for that table, so you must measure the table with the setting you actually ship.

modelBuilder.Entity<Blog>()
    .ToTable(tb => tb.UseSqlOutputClause(false));

Version and workload

Microsoft’s EF Core 7 release notes report that SaveChanges and SaveChangesAsync can be up to four times faster than in EF Core 6 in some scenarios. Microsoft attributes most of those improvements to the following, quoted verbatim: “Performing fewer roundtrips to the database; Generating faster SQL.” That claim is a whole-save comparison across scenarios. It is not an identity-only benchmark, and it should not be read as a general gain for every insert.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

How to measure the cost in your own application

A useful measurement isolates the save path and records what the database actually receives.

  1. Pin the exact EF Core and SQL Server provider package versions, and record the SQL Server version and compatibility level.
  2. Reproduce the schema you run in production, including triggers, computed columns, and any table where you have set UseSqlOutputClause(false).
  3. Run the test over a network path that resembles production. A local database hides the latency that makes round trips expensive.
  4. Log the generated commands for one representative SaveChanges call. A simple way is to add optionsBuilder.LogTo(Console.WriteLine, LogLevel.Information) to a test context, then check whether an extra statement appears.
  5. Measure end-to-end SaveChanges latency and throughput for the same workload, with and without the setting you are evaluating.
  6. Compare the numbers only inside the same environment. Do not subtract results from different benchmarks to estimate the key-return step.

Options and the axes to compare

SQL Server supports identity columns, sequences, and EF Core’s client-generated sequential GUID keys in applicable configurations. EF Core also supports the Hi-Lo pattern, which takes key values from blocks. These approaches differ in when the key becomes known, whether generating it requires a database call, and what they imply for the schema and the application. Choose among them on relationship needs, database architecture, key properties, and measured write patterns, not on the assumption that they are interchangeable.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

When you compare them, check each option against the same set of questions:

  • When the key becomes available to the application.
  • Whether generating the key requires a database communication.
  • How many commands and round trips the principal and dependent rows need.
  • How the option interacts with insert batching.
  • Whether it works with triggers and generated columns in your tables.
  • The key type’s effect on index size and insert locality.
  • Observed latency and throughput in your deployment.

The official sources establish the mechanics behind these questions but do not provide a head-to-head benchmark of the options, so treat any ranking as something to verify in your own environment.

Common mistakes when estimating the cost

  • Assuming EF Core always runs an extra query for the identity. In the documented EF Core 7 simple SQL Server case, the key returns in the insert through OUTPUT INSERTED.
  • Quoting a single millisecond figure as the price of returning an identity. No portable identity-only number exists.
  • Reading the four-times figure as an insert speed-up. It describes SaveChanges in some scenarios compared with EF Core 6.
  • Expecting client-generated keys to remove all database overhead. They change when the key is known; they do not establish a universal end-to-end win.

“

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Crashes, No Sound, or Screen Glitches?Free driver scan
Windows Errors? Fix Them Before They SpreadFree repair scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.