2

我有一个 SQL Server 2008 数据库,geography其中有一列由System.Data.Entity.Spatial.DbGeographyEntity Framework 6.0.0-alpha3 生成。

现在我需要用SqlDataReader. 但我不知道该怎么做。使用旧上下文不是一种选择。我试图将其转换为DbGeography

Location = (DbGeography)reader.GetValue(index)

但我得到这个错误:

无法将“Microsoft.SqlServer.Types.SqlGeography”类型的对象转换为“System.Data.Entity.Spatial.DbGeography”类型

你有什么建议吗?

4

2 回答 2

2

嗯,这很简单。我只是感到困惑。但是,我不会删除问题,而是将答案发布给有相同问题的其他人。

// 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);
于 2014-08-14T20:14:54.140 回答
2

如果您的地理位置是一个点,您可以使用:

选择 MyColumn.Lat、MyColumn.Long ...

reader.GetDouble(0);
reader.GetDouble(1);
于 2014-09-12T18:31:30.687 回答