-
Notifications
You must be signed in to change notification settings - Fork 255
Description
If I have a data model like this:
class Project {
// other properties
public Metadata Metadata { get; set; }
}
class Metadata {
// other properties
public Point Location { get; set; }
}
where Metadata is mapped using a JSON column with EF Core's ToJson():
builder
.Entity<Project>()
OwnsOne(u => u.Metadata, b => b.ToJson()
And I create a Point with an SRID set (new Point(1, 2) { SRID = 4236 }), and then save, the resulting JSON in the db will look like this:
{ "Location": "POINT (-38.6736 -3.78897)" }
Instead, I would expect it to be
{ "Location": "SRID=4326;POINT (-38.6736 -3.78897)" }
This is in contrast to if I had Location mapped to its own top-level column - in this case, the SRID is preserved.
This causes errors like "Operation on mixed SRID geometries " if I try to use operations like WITHIN or CONTAINS involving this geometry-inside-JSON property and other geometry columns whose SRID is 4326, since the geometry-inside-json column will be considered to have SRID of 0.
I believe the issue is with https://github.com/npgsql/efcore.pg/blob/main/src/EFCore.PG.NTS/Storage/Internal/NpgsqlJsonGeometryWktReaderWriter.cs#L30 - this just delegates to geometry.ToText(). That method does not write SRID, because the SRID prefix is part of EWKT which is only supported by Postgres and not part of the standard. In contrast https://github.com/npgsql/efcore.pg/blob/main/src/EFCore.PG.NTS/Storage/Internal/NpgsqlGeometryTypeMapping.cs#L76 does write the SRID.
I have a workaround - I can add a .HasConversion to the mapping for Location to overwrite the serialization. But it would be nicer if it just worked out of the box