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

本文关键字:数据 参数 插入 数据库 DbGeography 何通过 ado net | 更新日期: 2023-09-27 18:31:32

我正在尝试通过 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));

插入数据库地理类型的方法是什么?

如何通过 ado.net 将 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 答案 此处.