Skip to content

Commit 35f5f07

Browse files
authored
Fix ST_Extent box2d cast being removed by EF simplifier (#3828)
1 parent 4f0ff60 commit 35f5f07

1 file changed

Lines changed: 6 additions & 3 deletions

File tree

src/EFCore.PG.NTS/Query/ExpressionTranslators/Internal/NpgsqlNetTopologySuiteAggregateMethodCallTranslatorPlugin.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,10 @@ public NpgsqlNetTopologySuiteAggregateMethodTranslator(
122122
if (method == EnvelopeCombineMethod)
123123
{
124124
// ST_Extent returns a PostGIS box2d, which isn't a geometry and has no binary output function.
125-
// Convert it to a geometry first.
125+
// We need to cast the result to geometry. The aggregate function must use 'box2d' as the store type
126+
// so that the cast to geometry is not stripped as a no-op by EF's SqlExpressionSimplifyingExpressionVisitor.
127+
var geometryMapping = GetMapping();
128+
126129
return _sqlExpressionFactory.Convert(
127130
_sqlExpressionFactory.AggregateFunction(
128131
"ST_Extent",
@@ -131,8 +134,8 @@ public NpgsqlNetTopologySuiteAggregateMethodTranslator(
131134
nullable: true,
132135
argumentsPropagateNullability: [false],
133136
typeof(Geometry),
134-
GetMapping()),
135-
typeof(Geometry), GetMapping());
137+
geometryMapping?.WithStoreTypeAndSize("box2d", null)),
138+
typeof(Geometry), geometryMapping);
136139
}
137140

138141
if (method == UnionMethod || method == GeometryCombineMethod)

0 commit comments

Comments
 (0)