Commit 733d1734 authored by Nigel's avatar Nigel Committed by Marc Gravell

When reading Geometry and Geography values from the database, we need to...

When reading Geometry and Geography values from the database, we need to specify the SRID explicitly to retain it in the DbGeometry / DbGeography type. (#1038)

merging, thanks, and sorry for delay
parent 3bf2b4f7
......@@ -51,7 +51,7 @@ public override DbGeography Parse(object value)
if (value == null || value is DBNull) return null;
if (value is SqlGeography geo)
{
return DbGeography.FromBinary(geo.STAsBinary().Value);
return DbGeography.FromBinary(geo.STAsBinary().Value, geo.STSrid.Value);
}
return DbGeography.FromText(value.ToString());
}
......
......@@ -51,7 +51,7 @@ public override DbGeometry Parse(object value)
if (value == null || value is DBNull) return null;
if (value is SqlGeometry geo)
{
return DbGeometry.FromBinary(geo.STAsBinary().Value);
return DbGeometry.FromBinary(geo.STAsBinary().Value, geo.STSrid.Value);
}
return DbGeometry.FromText(value.ToString());
}
......
#if ENTITY_FRAMEWORK
using System;
using System.Data.Entity.Spatial;
using System.Linq;
using Xunit;
namespace Dapper.Tests.Providers
......@@ -16,7 +18,7 @@ public EntityFrameworkTests()
public void Issue570_DbGeo_HasValues()
{
EntityFramework.Handlers.Register();
const string redmond = "POINT (122.1215 47.6740)";
const string redmond = "POINT (-122.1215 47.6740)";
DbGeography point = DbGeography.PointFromText(redmond, DbGeography.DefaultCoordinateSystemId);
DbGeography orig = point.Buffer(20);
......@@ -34,6 +36,29 @@ public void Issue22_ExecuteScalar_EntityFramework()
var geo2 = connection.ExecuteScalar<DbGeography>("select @geo", new { geo });
Assert.NotNull(geo2);
}
[Fact]
public void TestGeometryParsingRetainsSrid()
{
const int srid = 27700;
var s = $@"DECLARE @EdinburghPoint GEOMETRY = geometry::STPointFromText('POINT(258647 665289)', {srid});
SELECT @EdinburghPoint";
var edinPoint = connection.Query<DbGeometry>(s).Single();
Assert.NotNull(edinPoint);
Assert.Equal(srid, edinPoint.CoordinateSystemId);
}
[Fact]
public void TestGeographyParsingRetainsSrid()
{
const int srid = 4324;
var s = $@"DECLARE @EdinburghPoint GEOGRAPHY = geography::STPointFromText('POINT(-3.19 55.95)', {srid});
SELECT @EdinburghPoint";
var edinPoint = connection.Query<DbGeography>(s).Single();
Assert.NotNull(edinPoint);
Assert.Equal(srid, edinPoint.CoordinateSystemId);
}
}
}
#endif
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment