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
As a follow up to #13752, I'm attempting to use HasDbFunction with my GeoCoordinate value object to call the database function STDistance.
I already have a value converter as defined below:
usingMicrosoft.EntityFrameworkCore;usingMicrosoft.EntityFrameworkCore.Storage.ValueConversion;usingNetTopologySuite;usingNetTopologySuite.Geometries;internalsealedclassGeoCoordinateConverter:ValueConverter<GeoCoordinate,Point>{privatestaticreadonlyGeometryFactory_geoFactory=NtsGeometryServices.Instance.CreateGeometryFactory(4326);publicGeoCoordinateConverter():base(valueObject =>ConvertToValue(valueObject), value =>ConvertToGeoCoordinate(value)){}privatestaticPointConvertToValue(GeoCoordinategeoCoordinate){varcoordinate=newCoordinate(geoCoordinate.Longitude.ToDouble(),geoCoordinate.Latitude.ToDouble());varpoint=_geoFactory.CreatePoint(coordinate);returnpoint;}privatestaticGeoCoordinateConvertToGeoCoordinate(Pointpoint){varcoordinate=point.Coordinate;returnGeoCoordinate.Convert(coordinate.Y,coordinate.X);}}internalstaticclassGeoCoordinateExtensions{publicstaticvoidConfigureGeoCoordinate(thisModelConfigurationBuilderconfigurationBuilder){configurationBuilder.Properties<GeoCoordinate>().HaveConversion<GeoCoordinateConverter>().HaveColumnType("geography");}}
The value converter does work when reading and writing entities that contain the GeoCoordinate value object. However, when defining the database function as below:
modelBuilder.HasDbFunction(typeof(MyDbContext).GetMethod(nameof(DistanceInMeters),new[]{typeof(GeoCoordinate),typeof(GeoCoordinate)})!,
b =>b.HasTranslation(
e =>newSqlFunctionExpression(e.First(),"STDistance",false,false,typeof(double),null)));
The following exception is thrown when the DbContext is instantiated:
System.InvalidOperationException: The parameter 'coordinate1' for theDbFunction
'MyDbContext.DistanceInMeters(GeoCoordinate,GeoCoordinate)' has aninvalid type 'GeoCoordinate'.
Ensure the parameter type can be mapped by thecurrentprovider.atMicrosoft.EntityFrameworkCore.Infrastructure.RelationalModelValidator.
ValidateDbFunctions(IModelmodel,IDiagnosticsLogger`1logger)
It seems that HasDbFunction does not take value converters into consideration.
Note: This is being developed and tested in .NET 8.0 with EF Core 8.0.
The text was updated successfully, but these errors were encountered:
I confirmed in a different project that there seems to be absolutely no way to configure a User Defined Function in such a way to make it possible to use parameters that are not a C# primitive type such as int, decimal, or string.
As a follow up to #13752, I'm attempting to use
HasDbFunction
with myGeoCoordinate
value object to call the database functionSTDistance
.I already have a value converter as defined below:
The value converter does work when reading and writing entities that contain the
GeoCoordinate
value object. However, when defining the database function as below:The following exception is thrown when the DbContext is instantiated:
It seems that
HasDbFunction
does not take value converters into consideration.Note: This is being developed and tested in .NET 8.0 with EF Core 8.0.
The text was updated successfully, but these errors were encountered: