Skip to content

Document Cosmos DB id mapping changes in EF Core 9.0 and add SDK migration scripts#5414

Open
AndriySvyryd with Copilot wants to merge 6 commits into
mainfrom
copilot/doc-cosmos-id-mapping-changes-9-0
Open

Document Cosmos DB id mapping changes in EF Core 9.0 and add SDK migration scripts#5414
AndriySvyryd with Copilot wants to merge 6 commits into
mainfrom
copilot/doc-cosmos-id-mapping-changes-9-0

Conversation

Copilot AI commented Jul 3, 2026

Copy link
Copy Markdown
Contributor

Fixes #4883
Fixes #4821

EF Core 9.0 changed how the Azure Cosmos DB provider maps entity keys to the JSON id property and removed the discriminator from id by default. This PR adds documentation for those changes and provides Azure Cosmos DB SDK code samples to migrate existing documents.

modeling.md — IDs and keys section

  • Explains that in EF 9.0 the entity key property IS the JSON id (no duplicate storage)
  • Shows the resulting document format
  • Adds a Shadow id subsection covering when EF falls back to a synthesized shadow id, and how to opt in explicitly via HasShadowId() / HasShadowIds()

breaking-changes.md — Migration code samples

Adds SDK-based migration code for each breaking change, for users who want to adopt the new defaults rather than revert:

Rename Discriminator$type (non-breaking ReplaceItemAsync):

item["$type"] = item["Discriminator"]!.DeepClone();
item.Remove("Discriminator");
await container.ReplaceItemAsync(item, id, partitionKey);

Strip discriminator prefix from id (requires delete + re-create since Cosmos DB prohibits id updates):

var newId = oldId[(oldId.IndexOf('|') + 1)..];
item["id"] = JsonValue.Create(newId);
await container.CreateItemAsync(item, partitionKey);
await container.DeleteItemAsync<JsonObject>(oldId, partitionKey);

Remove redundant key property (now that id and the key property are the same):

item.Remove("Id");
await container.ReplaceItemAsync(item, id, partitionKey);

Each sample includes notes on partition key configuration and idempotency considerations for the delete+re-create pattern.

Copilot AI changed the title [WIP] Document Cosmos id mapping changes in 9.0 with breaking change note Document Cosmos DB id mapping changes in EF Core 9.0 and add SDK migration scripts Jul 3, 2026
Copilot AI requested a review from AndriySvyryd July 3, 2026 02:03
@AndriySvyryd AndriySvyryd requested a review from Copilot July 3, 2026 02:11

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates EF Core 9.0 Cosmos DB documentation to explain the new id/key mapping behavior and adds Azure Cosmos DB SDK migration snippets to help users update existing documents to the new defaults.

Changes:

  • Document EF Core 9.0’s Cosmos id mapping (key maps directly to JSON id) and introduce guidance for shadow id usage.
  • Add Cosmos SDK migration examples for renaming Discriminator$type, rewriting id values, and removing redundant key properties.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.

File Description
entity-framework/core/what-is-new/ef-core-9.0/breaking-changes.md Adds Cosmos SDK-based migration snippets for EF Core 9.0 Cosmos breaking changes.
entity-framework/core/providers/cosmos/modeling.md Updates Cosmos modeling docs to describe EF Core 9.0 key/id mapping and shadow id behavior.

Comment thread entity-framework/core/providers/cosmos/modeling.md Outdated
Comment thread entity-framework/core/providers/cosmos/modeling.md
Comment thread entity-framework/core/what-is-new/ef-core-9.0/breaking-changes.md Outdated
Comment thread entity-framework/core/what-is-new/ef-core-9.0/breaking-changes.md Outdated

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

Comment thread entity-framework/core/what-is-new/ef-core-9.0/breaking-changes.md Outdated
Comment thread entity-framework/core/what-is-new/ef-core-9.0/breaking-changes.md Outdated
AndriySvyryd and others added 2 commits July 2, 2026 21:46
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
@AndriySvyryd AndriySvyryd marked this pull request as ready for review July 3, 2026 04:57
Copilot AI review requested due to automatic review settings July 3, 2026 04:57
@AndriySvyryd AndriySvyryd requested a review from cincuranet July 3, 2026 04:58

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

Comment on lines +454 to +457
if (item.ContainsKey("Discriminator"))
{
item["$type"] = item["Discriminator"]!.DeepClone();
item.Remove("Discriminator");
Comment on lines 30 to +32
Azure Cosmos DB requires all documents to have an `id` JSON property which uniquely identifies them. Like other EF providers, the EF Azure Cosmos DB provider will attempt to find a property named `Id` or `<type name>Id`, and configure that property as the key of your entity type, mapping it to the `id` JSON property. You can configure any property to be the key property by using <xref:Microsoft.EntityFrameworkCore.Metadata.Builders.EntityTypeBuilder`1.HasKey*>; see [the general EF documentation on keys](xref:core/modeling/keys) for more information.

Starting with EF 9.0, the entity's key property is directly mapped to the JSON `id` property — they are one and the same property in the document. This means the key value is not stored twice. For example, a `Blog` entity with `Id = 8` produces a document where the `id` property contains the string value `"8"`:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Document Cosmos id mapping changes in 9.0 Add workarounds for Cosmos breaking changes

3 participants