通过ado.net插入DBGeography类型的正确方法是什么

111*_*110 8 c# sql-server ado.net entity-framework sqlgeography

我正在尝试DBGeography通过ado.net 插入类型,但是没有运气。
这是我得到的错误:

从对象类型System.Data.Entity.Spatial.DbGeography到已知的托管提供程序本机类型不存在映射。

要么:

指定的类型未在目标服务器上注册。System.Data.Entity.Spatial.DbGeography,EntityFramework,Version = 6.0.0.0,Culture = neutral,PublicKeyToken = b77a5c561934e089。

这是我从db获取它时所做的,并且工作正常:

dynamic temp = reader.GetValue(3);

                var text = string.Format("POINT({0:R} {1:R})", temp.Long, temp.Lat);
                var srid = temp.STSrid.Value;
                this.Coordinates = System.Data.Entity.Spatial.DbGeography.PointFromText(text, srid);
Run Code Online (Sandbox Code Playgroud)

但是插入不起作用:

updateCommand.Parameters.AddWithValue("@Coordinates", store.Coordinates);
// or ...
SqlParameter p = new SqlParameter();
                    p.ParameterName = "@Coordinates";
                    p.Value = store.Coordinates;
                    p.SqlDbType = System.Data.SqlDbType.Udt;
                    p.UdtTypeName = "geography";
                    updateCommand.Parameters.Add(p);
Run Code Online (Sandbox Code Playgroud)

怎么了

Ali*_*ami 6

DbGeography是为EntityFrameworknot 设计的类型ADO.NET。尝试SqlGeography通过SqlGeography.Parse(SqlString)方法解析一个众所周知的文本模块,这应该可以解决您的问题。

dynamic temp = reader.GetValue(3);
var text = string.Format("POINT({0:R} {1:R})", temp.Long, temp.Lat);
var coordinate= SqlGeography.Parse(text );

SqlParameter p = new SqlParameter();
                 p.ParameterName = "@Coordinates";
                 p.Value = coordinate;
                 p.SqlDbType = System.Data.SqlDbType.Udt;
                 p.UdtTypeName = "geography";

updateCommand.Parameters.Add(p);
Run Code Online (Sandbox Code Playgroud)

TL; DR:

https://docs.microsoft.com/zh-cn/bingmaps/v8-web-control/modules/well-known-text-module

众所周知的文本(WKT)是一个开放式地理空间联盟(OGC)标准,用于以文本格式表示空间数据。大多数符合OGC的系统都支持“已知文本”。SQL Server 2008、2012和SQL Azure中的空间功能可以轻松地在数据库中的空间对象和WKT之间进行转换。WKT只能存储单个空间对象的信息,并且这种空间数据格式通常用作较大文件格式或Web服务响应的一部分。以下是表示为“熟知的文本”的每种几何类型的示例,以及解析“熟知的文本”字符串时生成的等效Bing Maps类的示例。

知名的文字模块