You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
For PostgreSQL extensions there is a CASCADE parameter for including dependencies as noted in CREATE EXTENSION. Would it be desirable to include an overload on HasPostgresExtension to signify installing dependencies?
Have you a specific instance where you need this? We could also add this always if we want to (not that HasPostgresExtension already adds IF NOT EXISTS, so it does not directly correspond to PG CREATE EXTENSIONS.
The earthdistance extension relies on the cube extension, so the following is necessitated. Not a deal breaker, but Just thought it might be a nice addition.
Sure, feel free to submit a PR. I'd simply always add the CASCADE rather than adding another parameter - at least I don't see any downside for doing so.
//earth is a constrained version of cubepublicNpgsqlCubeLatLngToEarth(doublelatitude,doublelongitude)=>thrownewNotSupportedException();publicdoubleEarthDistance(NpgsqlCubelocation1,NpgsqlCubelocation2)=>thrownewNotSupportedException();modelBuilder.HasDbFunction(typeof(MyContext).GetMethod(nameof(LatLngToEarth),[typeof(double),typeof(double)])!).HasName("ll_to_earth");modelBuilder.HasDbFunction(typeof(MyContext).GetMethod(nameof(EarthDistance),[typeof(NpgsqlCube),typeof(NpgsqlCube)])!).HasName("earth_distance");
Point-Based Earth Distances
For the SQL SELECT point(point1_lat, point1_lon) <@> point(point2_lat, point2_lon);
I would like to get it working using Mapping a method to a custom SQL. I could create a new NpgsqlPoint(x, y) but I would be stuck on the <@> operator.
I see you have something similar for the cube distance function. Any suggestions on defining this new operator, or would this necessitate changes to the provider?
For more complicated SQL translations, you should be able to get by with custom SQL translation as explained in this section of the docs). For the actual expression representing <@>, you should be able to use PgUnknownBinaryExpression.
Though I'd consider packaging all the earthdistance in a clean plugin, which would contain an IMethodTranslator, and which you'd enable via a single EnableEarthDistance() in the DbContext options; take a look at the NodaTime/NetTopologySuite plugins to see how that looks (it's really simple, there's just a bit of boilerplate that's needed). At that point you can even publish the plugin as a nuget package for others to use.
There's generally little translation for the built-in geometry types (point, linestring) since they're pretty rarely-used (compared to PostGIS).
Specifically for constructing a point, I'd add a constructor translation (so for new Point(x, y)) - that probably makes sense for all the geometry types. But if you're interested in these types, a more general pass for missing translation also makes sense - I'm sure there's various other stuff beyond construction that could be done.
Activity
roji commentedon Dec 7, 2025
Have you a specific instance where you need this? We could also add this always if we want to (not that HasPostgresExtension already adds
IF NOT EXISTS, so it does not directly correspond to PGCREATE EXTENSIONS.mguinness commentedon Dec 7, 2025
The earthdistance extension relies on the cube extension, so the following is necessitated. Not a deal breaker, but Just thought it might be a nice addition.
roji commentedon Dec 8, 2025
Sure, feel free to submit a PR. I'd simply always add the CASCADE rather than adding another parameter - at least I don't see any downside for doing so.
mguinness commentedon Dec 9, 2025
On a related note, looking at how best to translate the earthdistance functions:
Cube-Based Earth Distances
For the SQL
SELECT earth_distance( ll_to_earth(point1_lat, point1_lon), ll_to_earth(point2_lat, point2_lon) );I was able to get it working using the code below
With the functions defined in the DbContext
Point-Based Earth Distances
For the SQL
SELECT point(point1_lat, point1_lon) <@> point(point2_lat, point2_lon);I would like to get it working using Mapping a method to a custom SQL. I could create a new
NpgsqlPoint(x, y)but I would be stuck on the<@>operator.I see you have something similar for the cube distance function. Any suggestions on defining this new operator, or would this necessitate changes to the provider?
efcore.pg/src/EFCore.PG/Query/Expressions/Internal/PgBinaryExpression.cs
Line 152 in 539b255
roji commentedon Dec 9, 2025
For more complicated SQL translations, you should be able to get by with custom SQL translation as explained in this section of the docs). For the actual expression representing
<@>, you should be able to use PgUnknownBinaryExpression.Though I'd consider packaging all the earthdistance in a clean plugin, which would contain an IMethodTranslator, and which you'd enable via a single EnableEarthDistance() in the DbContext options; take a look at the NodaTime/NetTopologySuite plugins to see how that looks (it's really simple, there's just a bit of boilerplate that's needed). At that point you can even publish the plugin as a nuget package for others to use.
mguinness commentedon Dec 10, 2025
Thanks
PgUnknownBinaryExpressionwas the missing piece to get it working.mguinness commentedon Dec 10, 2025
Is there a reason
NpgsqlPointdoesn't have a translation to point? It's trivial to add, but it would be good to include in provider.roji commentedon Dec 10, 2025
There's generally little translation for the built-in geometry types (point, linestring) since they're pretty rarely-used (compared to PostGIS).
Specifically for constructing a
point, I'd add a constructor translation (so fornew Point(x, y)) - that probably makes sense for all the geometry types. But if you're interested in these types, a more general pass for missing translation also makes sense - I'm sure there's various other stuff beyond construction that could be done.[-]Dependencies option for HasPostgresExtension[/-][+]Add CASCADE when installing a PG extensions[/+]Change migration SQL to include CASCADE for CREATE EXTENSION (npgsql#…