2014-11-23 120 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

相关问题