-
Notifications
You must be signed in to change notification settings - Fork 3.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unable to use Microsoft.Azure.Cosmos.Spatial.Point in Entity Getting System.InvalidOperationException #20479
Comments
Duplicate of #17317 |
Can someone please give me a solution? |
@xtellurian Can you please give me the workaround for this problem with solution. |
hey @aliiqbalIntelligenes I sure can. I did something like this: public void Configure(EntityTypeBuilder<MyClassWithGeoJson> builder)
{
builder.Property(e => e.GeoLocation).HasJsonConversion();
} and the geolocation class public class GeoLocation
{
public GeoLocation(double lon, double lat)
{
Type = "Point";
if (lat > 90 || lat < -90) { throw new ArgumentException("A latitude coordinate must be a value between -90.0 and +90.0 degrees."); }
if (lon > 180 || lon < -180) { throw new ArgumentException("A longitude coordinate must be a value between -180.0 and +180.0 degrees."); }
Coordinates = new double[2] { lon, lat };
}
[JsonProperty("type")]
public string Type { get; set; }
[JsonProperty("coordinates")]
public double[] Coordinates { get; set; }
public double? Lat() => Coordinates?[1];
public double? Lon() => Coordinates?[0];
} where the extension method HasJsonConversion is as follows: public static PropertyBuilder<T> HasJsonConversion<T>(this PropertyBuilder<T> propertyBuilder)
{
ValueConverter<T, string> converter = new ValueConverter<T, string>(
v => JsonConvert.SerializeObject(v),
v => JsonConvert.DeserializeObject<T>(v));
ValueComparer<T> comparer = new ValueComparer<T>(
(l, r) => JsonConvert.SerializeObject(l) == JsonConvert.SerializeObject(r),
v => v == null ? 0 : JsonConvert.SerializeObject(v).GetHashCode(),
v => JsonConvert.DeserializeObject<T>(JsonConvert.SerializeObject(v)));
propertyBuilder.HasConversion(converter);
propertyBuilder.Metadata.SetValueConverter(converter);
propertyBuilder.Metadata.SetValueComparer(comparer);
return propertyBuilder;
} |
@xtellurian Thank you so much for the help. Then i can query like this ?
|
@aliiqbalIntelligenes I'm not sure |
@xtellurian Actually i want to query for users who are within specific kilometers. |
@xtellurian When data is in this format in cosmos db:
Then this query runs fine
But when saving in this format:
Query not returning any thing. will you help me on this? |
I'm sorry @aliiqbalIntelligenes but I don't know |
@aliiqbalIntelligenes Full support for spatial types in Cosmos is not yet implemented. That's what issue #17317 is tracking. While @aliiqbalIntelligenes's workaround will allow you to store and retrieve values from the database, support for for actually running this type of query will require #17317 to be implemented. |
@ajcvickers Thanks for clarification. When can we expect full support for spatial types in Cosmos? Any time period ? |
@aliiqbalIntelligenes #17317 is in the Backlog milestone. This means that it is not planned for the next release (EF Core 5.0). We will re-assess the backlog following the this release and consider it at that time. However, keep in mind that there are many other high priority features with which it will be competing for resources. |
@ajcvickers Ok Thanks. |
I'm using asp.net core 3.1 with entity framework core cosmos db provider. In my model I've a property for LocationCoordinates of type pointer.
I'm using point class in my model class ApplicationUser. Microsoft.Azure.Cosmos.Spatial.Point for saving coordinates latitude & longitude.
CosmosDb Package I'm using https://www.nuget.org/packages/Microsoft.EntityFrameworkCore.Cosmos/
Please help.
Entity Class:
DbContext Class:
Startup Class:
Project File:
Error:
The text was updated successfully, but these errors were encountered: