繁体   English   中英

如何通过ado.net将DbGeography数据参数插入数据库

[英]How to insert DbGeography data parameter to database via ado.net

我正在尝试通过ado.net在数据库中插入DbGeography数据类型,但出现错误。 我得到的错误是:

Additional information: No mapping exists from object type System.Data.Entity.Spatial.DbGeography to a known managed provider native type.

我使用的代码:实体:

public class Position{...
public DbGeography Coordinates { get; set; }
...

数据库更新:

position.Coordinates = DbGeography.FromText(string.Format("POINT({0} {1})", _longitude, _latitude));
...
cmd.CommandText = @"UPDATE Positions SET [Coordinates] = @Coordinates WHERE PositionId = 1";
cmd.Parameters.Add(new SqlParameter("@Coordinates", store.Coordinates));

插入DbGeography类型的方式是什么?

这可能是两件事之一。 我没有尝试通过ADO.NET直接将DbGeography发送到SQL,但是如果可以,则需要在SqlParameter对象上设置其他属性,如下所示:

SqlParameter p = new SqlParameter();
p.Name = "@Coordinates";
p.Value = store.Coordinates;
p.SqlDbType = SqlDbType.Udt;
p.UdtTypeName = "geography";

如果仍然不起作用,则需要将DbGeography实例转换为SqlGeography。 要获得帮助,请在此处查看我以前的SO答案。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM