Repository: dotnet/efcore
File: src/EFCore.SqlServer/Query/Internal/SqlServerQueryableMethodTranslatingExpressionVisitor.cs (C#)

Code before:
        foreach (var property in structuralType.GetPropertiesInHierarchy())
        {
            if (property.GetJsonPropertyName() is { } jsonPropertyName)
            {
                columnInfos.Add(
                    new SqlServerOpenJsonExpression.ColumnInfo
                    {
                        Name = jsonPropertyName,
                        TypeMapping = property.GetRelationalTypeMapping(),
                        Path = [new PathSegment(jsonPropertyName)],
                        AsJson = property.GetRelationalTypeMapping().ElementTypeMapping is not null
                    });
            }
        }

        var containerColumnName = structuralType.GetContainerColumnName();
        var containerColumnCandidates = structuralType.ContainingEntityType.GetTableMappings()
            .SelectMany(m => m.Table.Columns)
            .Where(c => c.Name == containerColumnName)
            .ToList();

        var containerColumn = containerColumnCandidates switch
        {
            [var c] => c,
            [] => throw new UnreachableException($"No container column found in relational model for {structuralType.DisplayName()}"),
            _ => throw new InvalidOperationException(
                $"Multiple columns with JSON container name '{containerColumnName}' on entity type '{structuralType.ContainingEntityType.DisplayName()}'")
        };

        var nestedJsonPropertyNames = jsonQueryExpression.StructuralType switch
        {
            IEntityType entityType
                => entityType.GetNavigationsInHierarchy()
                    .Where(n => n.ForeignKey.IsOwnership
                        && n.TargetEntityType.IsMappedToJson()
                        && n.ForeignKey.PrincipalToDependent == n)
                    .Select(n => n.TargetEntityType.GetJsonPropertyName() ?? throw new UnreachableException()),

            IComplexType complexType
                => complexType.GetComplexProperties().Select(p => p.ComplexType.GetJsonPropertyName() ?? throw new UnreachableException()),

            _ => throw new UnreachableException()
        };

        foreach (var jsonPropertyName in nestedJsonPropertyNames)
        {
            columnInfos.Add(
                new SqlServerOpenJsonExpression.ColumnInfo
                {
                    Name = jsonPropertyName,
                    TypeMapping = containerColumn.StoreTypeMapping,

Review comment (suggestion):
This should be UnreachableException too. We validate against it

Code after:
        foreach (var property in structuralType.GetPropertiesInHierarchy())
        {
            if (property.GetJsonPropertyName() is { } jsonPropertyName)
            {
                columnInfos.Add(
                    new SqlServerOpenJsonExpression.ColumnInfo
                    {
                        Name = jsonPropertyName,
                        TypeMapping = property.GetRelationalTypeMapping(),
                        Path = [new PathSegment(jsonPropertyName)],
                        AsJson = property.GetRelationalTypeMapping().ElementTypeMapping is not null
                    });
            }
        }

        // Find the container column in the relational model to get its type mapping
        // Note that we assume exactly one column with the given name mapped to the entity (despite entity splitting).
        // See #36647 and #36646 about improving this.
        var containerColumnName = structuralType.GetContainerColumnName();
        var containerColumn = structuralType.ContainingEntityType.GetTableMappings()
            .SelectMany(m => m.Table.Columns)
            .Where(c => c.Name == containerColumnName)
            .Single();

        var nestedJsonPropertyNames = jsonQueryExpression.StructuralType switch
        {
            IEntityType entityType
                => entityType.GetNavigationsInHierarchy()
                    .Where(n => n.ForeignKey.IsOwnership
                        && n.TargetEntityType.IsMappedToJson()
                        && n.ForeignKey.PrincipalToDependent == n)
                    .Select(n => n.TargetEntityType.GetJsonPropertyName() ?? throw new UnreachableException()),

            IComplexType complexType
                => complexType.GetComplexProperties().Select(p => p.ComplexType.GetJsonPropertyName() ?? throw new UnreachableException()),

            _ => throw new UnreachableException()
        };

        foreach (var jsonPropertyName in nestedJsonPropertyNames)
        {
            columnInfos.Add(
                new SqlServerOpenJsonExpression.ColumnInfo
                {
                    Name = jsonPropertyName,
                    TypeMapping = containerColumn.StoreTypeMapping,
                    Path = [new PathSegment(jsonPropertyName)],
                    AsJson = true
                });
        }