是否可以将 SqlGeography 与 Linq to Sql 一起使用?
我在尝试使用 Microsoft.SqlServer.Types.SqlGeography 时遇到了很多问题。我很清楚 Linq to Sql 对此的支持不是很好。我尝试了多种方法,从预期的方法开始(geography
的数据库类型、SqlGeography
的 CLR 类型)。这会产生 NotSupportedException
,该异常通过博客得到了广泛讨论。
然后,我将 geography 列视为 varbinary(max) ,因为 geography 是存储为二进制的 UDT。这似乎工作正常(使用一些二进制读写扩展方法)。
然而,我现在遇到了一个相当模糊的问题,这似乎没有发生在很多其他人身上。
System.InvalidCastException:无法将“Microsoft.SqlServer.Types.SqlGeography”类型的对象转换为“System.Byte[]”类型。
迭代查询时,ObjectMaterializer
会引发此错误。它似乎仅当包含地理列的表隐式包含在查询中时才会发生(即使用 EntityRef<> 属性进行联接)。
System.Data.Linq.SqlClient.ObjectReaderCompiler.ObjectReader`2.MoveNext()
我的问题:如果我将 geography
列检索为 varbinary(max)
,我可能会预计会出现相反的错误:无法将 byte[]
转换为 SqlGeography
。我会理解的。这个我不知道我确实在部分 LINQ to SQL 类上有一些隐藏二进制转换的属性...这可能是问题所在吗?
感谢任何帮助,我知道可能没有足够的信息。
附加:
- Visual Studio dbml 设计器中“服务器数据类型”=“地理”的“地理”列生成此错误:
指定的类型“地理”不是有效的提供程序类型。
- Visual Studio dbml 设计器中没有“服务器数据类型”的
geography
列会生成此错误:无法格式化节点“Value”以作为 SQL 执行。< /代码>
I've been having quite a few problems trying to use Microsoft.SqlServer.Types.SqlGeography
. I know full well that support for this in Linq to Sql is not great. I've tried numerous ways, beginning with what would the expected way (Database type of geography
, CLR type of SqlGeography
). This produces the NotSupportedException
, which is widely discussed via blogs.
I've then gone down the path of treating the geography
column as a varbinary(max)
, as geography
is a UDT stored as binary. This seems to work fine (with some binary reading and writing extension methods).
However, I'm now running into a rather obscure issue, which does not seem to have happened to many other people.
System.InvalidCastException: Unable to cast object of type 'Microsoft.SqlServer.Types.SqlGeography' to type 'System.Byte[]'.
This error is thrown from an ObjectMaterializer
when iterating through a query. It seems to occur only when the tables containing geography columns are included in a query implicitly (ie. using the EntityRef<>
properties to do joins).
System.Data.Linq.SqlClient.ObjectReaderCompiler.ObjectReader`2.MoveNext()
My question: If I'm retrieving the geography
column as varbinary(max)
, I might expect the reverse error: can't cast byte[]
to SqlGeography
. That I would understand. This I don't. I do have some properies on the partial LINQ to SQL classes that hide the binary conversion... could those be the issue?
Any help appreciated, and I know there's probably not enough information.
Extras:
- A
geography
column in the Visual Studio dbml Designer with 'Server Data Type' =geography
generates this error:The specified type 'geography' is not a valid provider type.
- A
geography
column in the Visual Studio dbml Designer with no 'Server Data Type' generates this error:Could not format node 'Value' for execution as SQL.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您只想使用 SqlGeography 跟踪点并利用 SQL Server 2008 的空间索引,那么您可以像其他人指出的那样,从 Linq to SQL 隐藏空间数据列并使用 UDF 或存储过程。假设您有一个包含纬度和经度字段的表 AddressFields。将该表添加到您的 DBML 文件中,并编写您想要设置纬度和经度字段的任何代码。然后,下面的 SQL 代码将向该表添加一个 Geo geogarphy 字段,并在数据库中创建一个触发器,根据纬度和经度字段自动设置 Geo 字段。同时,下面的代码还创建了其他有用的 UDF 和存储过程: DistanceBetween2(我已经有一个 DistanceBetween)返回 AddressField 中表示的地址与指定的纬度/经度对之间的距离; DistanceWithin 返回指定英里距离内所有地址字段的各个字段; UDFDistanceWithin 的作用与用户定义的函数相同(如果您想将其嵌入到更大的查询中,则很有用); UDFNearestNeighbors 从 AddressField 返回与最接近特定点的指定数量的邻居相对应的字段。 (使用 UDFNearestNeighbors 的原因之一是,如果您只是通过调用 DistanceBetween2 来调用 order,则 SQL Server 2008 将不会优化其对空间索引的使用。)
您需要通过将 AddressFields 更改为表并自定义该表中的字段来进行自定义。您想要返回的表(查看对 AddressFieldID 引用的代码)。然后,您可以在数据库上运行它,并将生成的存储过程和 UDF 复制到 DBML 中,然后可以在查询中使用它们。总的来说,这使您可以相当轻松地利用点的空间索引。
--[1]
--[2]
--[3] 创建索引
--[4] 创建过程 USP_SET_GEO_VALUE PARA 1 LATITUDE 2 LONGITUDE
--[5] 创建纬度/经度值更改/插入触发器 ---> SET GEOCODE
--[6] CREATE PROC USP_SET_GEO_VALUE_INITIAL_LOAD ---->仅一次运行
指定的两点之间的距离
--[7] 现有过程 DistanceBetween,返回由纬度/经度坐标对 。 --ALTER PROC DistanceBetween2
GO
--TEST
DistanceBetween2 12159,40.75889600,-73.99228900
--[8] 创建过程 USPDistanceWithin
-- 如果存在,则返回 AddressFields 表中的地址列表
(从 sysobjects 中选择名称,其中名称 = 'USPDistanceWithin' AND 类型 = 'P')
跌落程序 USPDistanceWithin
GO
--TEST
--3 英里内
USP距离38.90606200内,-76.92943500,3内
去
--5英里以内
USP距离38.90606200内,-76.92943500,5内
去
--10英里以内
USPDistanceWithin 38.90606200,-76.92943500,10
--[9] 创建函数 FNDistanceWithin
--
如果存在则从 AddressFields 表返回地址列表(从 sysobjects 中选择名称,其中名称 = 'UDFDistanceWithin' AND 类型 = 'TF')
DROP FUNCTION UDFDistanceWithin
GO
--TEST
--3 英里内
从 UDFDistanceWithin(38.90606200,-76.92943500,3) 中选择 *
去
--5英里以内
从 UDFDistanceWithin( 38.90606200,-76.92943500,5) 选择 *
去
--10英里以内
select * from UDFDistanceWithin( 38.90606200,-76.92943500,10)
--[9] CREATE FUNCTION UDFNearestNeighbors
--
如果存在则从 AddressFields 表返回地址列表(从 sysobjects 中选择名称,其中名称 = 'UDFNearestNeighbors' AND 类型 = 'TF')
删除函数 UDFNearestNeighbors
IF EXISTS(从 sysobjects 中选择名称,其中 name = 'numbers' AND xtype = 'u')
删除表编号
GO
--TEST
--50 个邻居
从 UDFNearestNeighbors(38.90606200,-76.92943500,50) 选择 *
去
--200个邻居
从 UDFNearestNeighbors 选择 *( 38.90606200,-76.92943500,200)
去
If all you want to do with SqlGeography is track points and take advantage of SQL Server 2008's spatial indices, you can, as others have noted, hide your spatial data column from Linq to SQL and use UDFs or stored procedures. Suppose that you have a table AddressFields that includes Latitude and Longitude fields. Add that table to your DBML file, and write any code that you want that sets the Latitude and Longitude fields. Then, the SQL code below will add a Geo geogarphy field to that table and create a trigger in the database that automatically sets the Geo field based on the Latitude and Longitude fields. Meanwhile, the code below also creates other useful UDFs and stored procedures: DistanceBetween2 (I already had a DistanceBetween) returns the distance between the address represented in an AddressField and a specified latitude/longitude pair; DistanceWithin returns various fields from all AddressFields within a specified mile distance; UDFDistanceWithin does the same as a user-defined function (useful if you want to embed this in a larger query); and UDFNearestNeighbors returns fields from AddressField corresponding to the specified number of neighbors nearest a particular point. (One reason for using UDFNearestNeighbors is that SQL Server 2008 won't optimize its use of a spatial index if you just call order by calling DistanceBetween2.)
You'll need to customize this by changing AddressFields to your table and customizing the fields from that table that you want returned (look in the code around references to AddressFieldID). You can then run this on your database and copy the resulting stored procedures and UDFs onto your DBML, and then you can use them in queries. Overall, this allows you to take advantage of a spatial index of points fairly easily.
--[1]
--[2]
--[3] CREATE INDEX
--[4] CREATE PROCEDURE USP_SET_GEO_VALUE PARA 1 LATITUDE 2 LONGITUDE
--[5] CREATE TRIGGER ON LAT/LONG VALUE CHANGE/INSERT ---> SET GEOCODE
--[6] CREATE PROC USP_SET_GEO_VALUE_INITIAL_LOAD ----> ONE TIME RUN ONLY
--[7] EXISTING PROC DistanceBetween, which returns the distance between two points specified
--by latitude/longitude coordinate pairs. --ALTER PROC DistanceBetween2
GO
--TEST
DistanceBetween2 12159,40.75889600,-73.99228900
--[8] CREATE PROCEDURE USPDistanceWithin
-- RETURNS LIST OF ADDRESSES FROM AddressFields table
IF EXISTS (SELECT name FROM sysobjects WHERE name = 'USPDistanceWithin' AND type = 'P')
DROP PROCEDURE USPDistanceWithin
GO
--TEST
--within 3 miles
USPDistanceWithin 38.90606200,-76.92943500,3
GO
--within 5 miles
USPDistanceWithin 38.90606200,-76.92943500,5
GO
--within 10 mile
USPDistanceWithin 38.90606200,-76.92943500,10
--[9] CREATE FUNCTION FNDistanceWithin
-- RETURNS LIST OF ADDRESSES FROM AddressFields table
IF EXISTS (SELECT name FROM sysobjects WHERE name = 'UDFDistanceWithin' AND type = 'TF')
DROP FUNCTION UDFDistanceWithin
GO
--TEST
--within 3 miles
select * from UDFDistanceWithin(38.90606200,-76.92943500,3)
GO
--within 5 miles
select * from UDFDistanceWithin( 38.90606200,-76.92943500,5)
GO
--within 10 mile
select * from UDFDistanceWithin( 38.90606200,-76.92943500,10)
--[9] CREATE FUNCTION UDFNearestNeighbors
-- RETURNS LIST OF ADDRESSES FROM AddressFields table
IF EXISTS (SELECT name FROM sysobjects WHERE name = 'UDFNearestNeighbors' AND type = 'TF')
DROP FUNCTION UDFNearestNeighbors
IF EXISTS (SELECT name FROM sysobjects WHERE name = 'numbers' AND xtype = 'u')
DROP TABLE numbers
GO
--TEST
--50 neighbors
select * from UDFNearestNeighbors(38.90606200,-76.92943500,50)
GO
--200 neighbors
select * from UDFNearestNeighbors( 38.90606200,-76.92943500,200)
GO
Linq to SQL 不支持空间类型。支持并不是“不太好”——而是根本不存在。
您可以将它们作为 BLOB 读取,但不能通过简单地更改 Linq to SQL 中的列类型来实现这一点。您需要使用
CAST
语句在数据库级别更改查询,以将列作为varbinary
返回。您可以通过添加计算的varbinary
列在表级别执行此操作,Linq 会很乐意将其映射到byte[]
。换句话说,某些 DDL 如下所示:
然后,从 Linq to SQL 类中删除
Location
列,并使用LocationData
代替。如果您随后需要访问实际的
SqlGeography
实例,则需要使用 STGeomFromWKB 和 STAsBinary。您可以通过扩展部分 Linq to SQL 实体类并添加自动转换属性来使此过程更加“自动”:
这假设
LocationData
是计算的varbinary< /代码> 列;您没有在 Linq to SQL 定义中包含“真实”
Location
列,而是以上面的临时方式添加它。另请注意,除了读取和写入之外,您无法对该列执行太多操作;如果您尝试实际查询它(即将其包含在
Where
谓词中),那么您只会得到类似的NotSupportedException
。Spatial types are not supported by Linq to SQL. Support is not "not great" - it's nonexistent.
You can read them as BLOBs, but you can't do that by simply changing the column type in Linq to SQL. You need to alter your queries at the database level to return the column as a
varbinary
, using theCAST
statement. You can do this at the table level by adding a computedvarbinary
column, which Linq will happily map to abyte[]
.In other words, some DDL like this:
Then, remove the
Location
column from your Linq to SQL class, and useLocationData
instead.If you then need access to the actual
SqlGeography
instance, you'll need to convert it to and from the byte array, using STGeomFromWKB and STAsBinary.You can make this process a bit more "automatic" by extending the partial Linq to SQL entity class and adding an auto-converting property:
This assumes that
LocationData
is the name of the computedvarbinary
column; you don't include the "real"Location
column in your Linq to SQL definition, you add it in the ad-hoc fashion above.Note also that you won't be able to do much with this column other than read and write to it; if you try to actually query on it (i.e. including it in a
Where
predicate) then you'll just get a similarNotSupportedException
.