I have a model that looks like this:
public class Facility
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public NetTopologySuite.Geometries.Point Location { get; set; }
}
Test code for adding a Point:
var testFacility = new Facility();
testFacility.Location = new NetTopologySuite.Geometries.Point(13.003725d, 55.604870d) { SRID = 3857 };
//Other values tested with the same error error
//testFacility.Location = new NetTopologySuite.Geometries.Point(13.003725d, 55.604870d);
//testFacility.Location = new NetTopologySuite.Geometries.Point(55.604870d, 13.003725d);
//var geometryFactory = NtsGeometryServices.Instance.CreateGeometryFactory(srid: 3857);
//var currentLocation = geometryFactory.CreatePoint(new Coordinate(13.003725d, 55.604870d));
//testFacility.Location = currentLocation;
db.Facilities.Add(testFacility);
//Exception on Save
db.SaveChanges();
I'm using the following NuGets, version 3.1.0
Microsoft.AspNetCore.Identity.EntityFrameworkCore
Microsoft.EntityFrameworkCore.SqlServer
Microsoft.EntityFrameworkCore.Tools
Microsoft.EntityFrameworkCore.SqlServer.NetTopologySuite
The exception I get on save is the following:
SqlException: The incoming tabular data stream (TDS) remote procedure call (RPC) protocol stream is incorrect. Parameter 7 ("@p6"): The supplied value is not a valid instance of data type geography. Check the source data for invalid values. An example of an invalid value is data of numeric type with scale greater than precision.
According to all documentation it should be X for longitude and Y for latitude so I don't think that is a problem. I tried to reverse the coordinates just in case but I got the same error as you can see in the examples I have tried.
https://learn.microsoft.com/en-us/ef/core/modeling/spatial
Lat = Y Long = X
https://gis.stackexchange.com/a/68856/71364
I can't find out anything obvious that seems wrong. Optionsbuilder is set up, the table is created with Data Type geography
that worked really well with DbGeography
for Entity Framework 6.
var optionsBuilder = new DbContextOptionsBuilder<ApplicationDbContext>();
optionsBuilder.UseSqlServer("Server=(localdb)\\mssqllocaldb;Database=TestDb;Trusted_Connection=True;MultipleActiveResultSets=true",
x => x.UseNetTopologySuite());
var db = new ApplicationDbContext(optionsBuilder.Options);
There is no specific cases to handle for a single Point
either what I can see in documentation for SQL server.
https://learn.microsoft.com/en-us/ef/core/modeling/spatial#sql-server
The coordinates I'm saving is from Google Maps and therefore EPSG 3857
is used.
What am I missing?
Point
class to the MSSQLgeometry
type, notgeography
. Hence when attempting to persist the record, NTS serializes ageometry
while MSSQL expects ageography
and everything explodes. – Margertmargerysys.spatial_reference_systems
– Fanya