Skip to content

Fix enum discriminator serialization inconsistency in Cosmos provider#36771

Closed
Copilot wants to merge 2 commits intorelease/10.0from
copilot/fix-34852
Closed

Fix enum discriminator serialization inconsistency in Cosmos provider#36771
Copilot wants to merge 2 commits intorelease/10.0from
copilot/fix-34852

Conversation

Copy link
Contributor

Copilot AI commented Sep 12, 2025

The Cosmos provider was incorrectly serializing enum discriminators as integers during entity updates, even when configured with a string converter. This caused entities to become unqueryable after updates.

Problem

When using enum discriminators with string conversion in the Cosmos provider:

  1. First save: Discriminator correctly saved as string (e.g., "ThingyTwo")
  2. Subsequent updates: Discriminator incorrectly saved as integer (e.g., 1)

This inconsistency broke queries that expected string discriminator values, making updated entities unloadable.

// Configuration
modelBuilder.Entity<Thingy>(b =>
{
    b.Property(e => e.Type).HasConversion<string>();
    b.HasDiscriminator(e => e.Type)
        .HasValue<DerivedThingy2>(ThingyType.ThingyTwo);
});

// After first save: { "Type": "ThingyTwo" } ✓
// After update: { "Type": 1 } ✗

Root Cause

The issue was in DocumentSource.ConvertPropertyValue() which used entry.GetCurrentProviderValue(property) for all properties. During updates, change tracking could corrupt discriminator property values, causing the method to return the raw enum integer instead of the converted string value.

Solution

Enhanced ConvertPropertyValue() to handle discriminator properties specially:

  • Detect discriminator properties using entry.EntityType.FindDiscriminatorProperty()
  • For discriminator properties, use the authoritative value from entry.EntityType.GetDiscriminatorValue()
  • Apply the property's type mapping converter to ensure correct serialization
  • Preserve existing behavior for all non-discriminator properties

This ensures discriminator values remain consistent regardless of change tracking state.

Fixes #34852.


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
Copilot AI changed the title [WIP] Discriminator on derived type is being incorrectly updated with integer value of enum (EF Core CosmosDB provider) Fix enum discriminator serialization inconsistency in Cosmos provider Sep 12, 2025
Copilot AI requested a review from AndriySvyryd September 12, 2025 23:46
@AndriySvyryd AndriySvyryd deleted the copilot/fix-34852 branch September 13, 2025 00:41
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.

Discriminator on derived type is being incorrectly updated with integer value of enum (EF Core CosmosDB provider)

2 participants