Skip to content

Commit 733d173

Browse files
Nij4t2mgravell
authored andcommitted
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
1 parent 3bf2b4f commit 733d173

File tree

3 files changed

+28
-3
lines changed

3 files changed

+28
-3
lines changed

Dapper.EntityFramework/DbGeographyHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public override DbGeography Parse(object value)
5151
if (value == null || value is DBNull) return null;
5252
if (value is SqlGeography geo)
5353
{
54-
return DbGeography.FromBinary(geo.STAsBinary().Value);
54+
return DbGeography.FromBinary(geo.STAsBinary().Value, geo.STSrid.Value);
5555
}
5656
return DbGeography.FromText(value.ToString());
5757
}

Dapper.EntityFramework/DbGeometryHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public override DbGeometry Parse(object value)
5151
if (value == null || value is DBNull) return null;
5252
if (value is SqlGeometry geo)
5353
{
54-
return DbGeometry.FromBinary(geo.STAsBinary().Value);
54+
return DbGeometry.FromBinary(geo.STAsBinary().Value, geo.STSrid.Value);
5555
}
5656
return DbGeometry.FromText(value.ToString());
5757
}

Dapper.Tests/Providers/EntityFrameworkTests.cs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#if ENTITY_FRAMEWORK
2+
using System;
23
using System.Data.Entity.Spatial;
4+
using System.Linq;
35
using Xunit;
46

57
namespace Dapper.Tests.Providers
@@ -16,7 +18,7 @@ public EntityFrameworkTests()
1618
public void Issue570_DbGeo_HasValues()
1719
{
1820
EntityFramework.Handlers.Register();
19-
const string redmond = "POINT (122.1215 47.6740)";
21+
const string redmond = "POINT (-122.1215 47.6740)";
2022
DbGeography point = DbGeography.PointFromText(redmond, DbGeography.DefaultCoordinateSystemId);
2123
DbGeography orig = point.Buffer(20);
2224

@@ -34,6 +36,29 @@ public void Issue22_ExecuteScalar_EntityFramework()
3436
var geo2 = connection.ExecuteScalar<DbGeography>("select @geo", new { geo });
3537
Assert.NotNull(geo2);
3638
}
39+
40+
[Fact]
41+
public void TestGeometryParsingRetainsSrid()
42+
{
43+
const int srid = 27700;
44+
var s = $@"DECLARE @EdinburghPoint GEOMETRY = geometry::STPointFromText('POINT(258647 665289)', {srid});
45+
SELECT @EdinburghPoint";
46+
var edinPoint = connection.Query<DbGeometry>(s).Single();
47+
Assert.NotNull(edinPoint);
48+
Assert.Equal(srid, edinPoint.CoordinateSystemId);
49+
}
50+
51+
[Fact]
52+
public void TestGeographyParsingRetainsSrid()
53+
{
54+
const int srid = 4324;
55+
var s = $@"DECLARE @EdinburghPoint GEOGRAPHY = geography::STPointFromText('POINT(-3.19 55.95)', {srid});
56+
SELECT @EdinburghPoint";
57+
var edinPoint = connection.Query<DbGeography>(s).Single();
58+
Assert.NotNull(edinPoint);
59+
Assert.Equal(srid, edinPoint.CoordinateSystemId);
60+
}
61+
3762
}
3863
}
3964
#endif

0 commit comments

Comments
 (0)