2

I have a SQL Server 2008 database with a geography column which is generated by System.Data.Entity.Spatial.DbGeography in Entity Framework 6.0.0-alpha3.

Now I need to read that column with a SqlDataReader. But I have no idea how to do this. Using the old context is not an option. I tried to cast it as DbGeography:

Location = (DbGeography)reader.GetValue(index)

But I get this error:

Unable to cast object of type 'Microsoft.SqlServer.Types.SqlGeography' to type 'System.Data.Entity.Spatial.DbGeography'

Do you have any suggestion?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
amiry jd
  • 27,021
  • 30
  • 116
  • 215

2 Answers2

2

Well, it was simple. I was just confused. But instead of deleting the question, I'll post the answer to others who they have the same question.

// read the value as dynamic:
dynamic temp = reader.GetValue(index);

// the temp contains Lat and Long properties:
var text = string.Format("POINT({0:R} {1:R})", temp.Long, temp.Lat);

// the temp also contains the STSrid as coordinate system id:
var srid = temp.STSrid.Value;

// the rest is really simple:
Location = System.Data.Entity.Spatial.DbGeography.PointFromText(text, srid);
amiry jd
  • 27,021
  • 30
  • 116
  • 215
2

If your geography is a point, you can use:

SELECT MyColumn.Lat, MyColumn.Long ...

reader.GetDouble(0);
reader.GetDouble(1);
Michael Diomin
  • 530
  • 3
  • 13