簡體   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