Repository: dotnet/efcore
File: src/EFCore/Metadata/Internal/EntityTypeExtensions.cs (C#)

Code before:
        var parentOriginalValueIndex = originalValueIndex;
        var parentShadowIndex = shadowIndex;
        var parentRelationshipIndex = relationshipIndex;
        var parentStoreGenerationIndex = storeGenerationIndex;

        if (complexProperty.IsCollection)
        {
            propertyIndex = 0;
            complexPropertyIndex = 0;
            complexCollectionIndex = 0;
            originalValueIndex = 0;
            shadowIndex = 0;
            relationshipIndex = 0;
            storeGenerationIndex = 0;
        }

        var complexType = complexProperty.ComplexType;
        var discriminatorProperty = ((IReadOnlyTypeBase)complexType).FindDiscriminatorProperty();

        foreach (IRuntimeProperty property in complexType.GetProperties())
        {
            var complexIndexes = new PropertyIndexes(
                index: propertyIndex++,
                originalValueIndex: property.RequiresOriginalValue() ? originalValueIndex++ : -1,
                // Discriminator properties on complex types are not included in entity-level shadow values
                shadowIndex: property.IsShadowProperty() && property != discriminatorProperty ? shadowIndex++ : -1,
                relationshipIndex: property.IsKey() || property.IsForeignKey() ? relationshipIndex++ : -1,
                storeGenerationIndex: property.MayBeStoreGenerated() ? storeGenerationIndex++ : -1);

            property.PropertyIndexes = complexIndexes;
        }

        CountComplexProperties(
            complexType.GetComplexProperties(),
            ref propertyIndex,
            ref complexPropertyIndex,
            ref complexCollectionIndex,
            ref originalValueIndex,
            ref shadowIndex,
            ref relationshipIndex,
            ref storeGenerationIndex);

        if (complexProperty.IsCollection)
        {
            ((IRuntimeComplexType)complexProperty.ComplexType).SetCounts(
                new PropertyCounts(
                    propertyIndex,
                    navigationCount: 0,
                    complexPropertyIndex,
                    complexCollectionIndex,
                    originalValueIndex,

Review comment (suggestion):
@copilot This isn't the right fix. Shadow discriminators on complex types should have and index like any other shadow property.

Code after:
        var parentOriginalValueIndex = originalValueIndex;
        var parentShadowIndex = shadowIndex;
        var parentRelationshipIndex = relationshipIndex;
        var parentStoreGenerationIndex = storeGenerationIndex;

        if (complexProperty.IsCollection)
        {
            propertyIndex = 0;
            complexPropertyIndex = 0;
            complexCollectionIndex = 0;
            originalValueIndex = 0;
            shadowIndex = 0;
            relationshipIndex = 0;
            storeGenerationIndex = 0;
        }

        var complexType = complexProperty.ComplexType;
        foreach (IRuntimeProperty property in complexType.GetProperties())
        {
            var complexIndexes = new PropertyIndexes(
                index: propertyIndex++,
                originalValueIndex: property.RequiresOriginalValue() ? originalValueIndex++ : -1,
                shadowIndex: property.IsShadowProperty() ? shadowIndex++ : -1,
                relationshipIndex: property.IsKey() || property.IsForeignKey() ? relationshipIndex++ : -1,
                storeGenerationIndex: property.MayBeStoreGenerated() ? storeGenerationIndex++ : -1);

            property.PropertyIndexes = complexIndexes;
        }

        CountComplexProperties(
            complexType.GetComplexProperties(),
            ref propertyIndex,
            ref complexPropertyIndex,
            ref complexCollectionIndex,
            ref originalValueIndex,
            ref shadowIndex,
            ref relationshipIndex,
            ref storeGenerationIndex);

        if (complexProperty.IsCollection)
        {
            ((IRuntimeComplexType)complexProperty.ComplexType).SetCounts(
                new PropertyCounts(
                    propertyIndex,
                    navigationCount: 0,
                    complexPropertyIndex,
                    complexCollectionIndex,
                    originalValueIndex,
                    shadowIndex,
                    relationshipIndex,
                    storeGenerationIndex));