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

After SaveChanges() or SaveChangesAsync() completes successfully, read the generated key from the entity you added—for example, entity.Id. The save method returns the number of entries written, not the new ID.

Read the entity’s ID after saving

Keep a reference to the entity, add it to the context, save the changes, and then read its key property:

var entity = new Blog { Name = "Example" };
context.Blogs.Add(entity);
context.SaveChanges();

int id = entity.Id;

For asynchronous code, await the save before reading the key:

var entity = new Blog { Name = "Example" };
await context.Blogs.AddAsync(entity);
await context.SaveChangesAsync();

int id = entity.Id;

Microsoft describes SaveChanges as saving changes made in the context to the database. Its return value is a count of state entries written; it is not the inserted row’s key.

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

Why the ID is available after SaveChanges

When a key is configured for database generation, Entity Framework may assign a temporary value while it tracks a new entity. During the save, the database generates the permanent value and EF updates the tracked entity with it. Read the property only after the save operation has completed successfully.

For SQL Server identity columns, EF Core’s documented insert pattern retrieves the generated value with scope_identity(). That is an example of provider-specific behavior, not a SQL statement to use for every database provider. See the EF Core SQL Server value-generation documentation.

If the ID is still zero or unexpected

  • Check the save completed: Make sure execution reached the line after SaveChanges() or the awaited SaveChangesAsync(). If saving fails, the generated database value is not available through the successful-save path.
  • Check key configuration: Confirm the entity’s key property is configured to be generated by the database if that is your intended setup. EF’s value-generation behavior depends on the model configuration and provider.
  • Check the database schema and mapping: Verify that the mapped column actually generates a value and that the entity property maps to that column. EF Core’s generated properties documentation explains the configuration options.
  • Check whether the key is client-generated: Some key setups create values on the client rather than obtaining an identity from the database. In that case, the appropriate value-generation configuration determines when the value appears.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Does this work in EF6 as well as EF Core?

The basic pattern is shared: add the entity, call the version’s save method, then read the key from that entity. EF6 and EF Core have different APIs and provider-specific details, so use documentation for the package and database provider in your project when changing key-generation configuration. The SQL Server identity example above is specifically from EF Core documentation.

Best Value
Sale
Programming Entity Framework
  • Used Book in Good Condition

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.

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