2014-11-23 122 views
1

我試圖通過ado.net在數據庫中插入DbGeography數據類型,但我收到錯誤消息。 錯誤,我得到的是:如何通過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; } 
... 

DB更新:

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類型的方式嗎?

回答

2

它可能是兩件事之一。我還沒有嘗試直接發送DbGeography通過ADO.NET到SQL,但如果你能,那麼你需要的的SqlParameter對象上設置附加屬性,像這樣:

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

如果仍然無法正常工作,那麼你需要將DbGeography實例轉換爲SqlGeography。爲了獲得幫助,請參閱我以前的回答here

相關問題