异常:不存在从对象类型 System.Data.Spatial.DbGeography 到已知托管提供程序本机类型的映射
Exception : No mapping exists from object type System.Data.Spatial.DbGeography to a known managed provider native type
当我尝试更新数据库中的地理位置值时,出现异常。
var requestUri = string.Format("http://maps.googleapis.com/maps/api/geocode/xml?address={0}&sensor=false", Uri.EscapeDataString(address));
var request = WebRequest.Create(requestUri);
var response = request.GetResponse();
var xdoc = System.Xml.Linq.XDocument.Load(response.GetResponseStream());
var result = xdoc.Element("GeocodeResponse").Element("result");
var locationElement = result.Element("geometry").Element("location");
var latitude = locationElement.Element("lat");
var longitude = locationElement.Element("lng");
double lat = Double.Parse(latitude.Value);
double lon = Double.Parse(longitude.Value);
var geo = string.Format(CultureInfo.InvariantCulture.NumberFormat, "POINT({0} {1})", lat, lon);
var geoLocation = DbGeography.PointFromText(geo, 4326);
SqlCommand updationCommand = connection.CreateCommand();
updationCommand.CommandText = "Update Communications SET [GeoLocation]=@GeoLoc Where [Id] =@Id";
updationCommand.Parameters.AddWithValue("@Id", row.Id);
updationCommand.Parameters.AddWithValue("@GeoLoc", geoLocation);
var param2 = updationCommand.CreateParameter();
豁免 - "No mapping exists from object type System.Data.Spatial.DbGeography to a known managed provider native type."
使用 SqlGeography 而不是 DbGeography,因为 DbGeography 旨在与 EF 一起使用。
尝试
SqlParameter p = new SqlParameter();
p.Name = "@GeoLoc";
p.Value = SqlGeography.Parse(geoLocation.AsText());
p.SqlDbType = SqlDbType.Udt;
p.UdtTypeName = "geography";
updationCommand.Parameters.Add(p);
更多信息在此 questions
当我尝试更新数据库中的地理位置值时,出现异常。
var requestUri = string.Format("http://maps.googleapis.com/maps/api/geocode/xml?address={0}&sensor=false", Uri.EscapeDataString(address));
var request = WebRequest.Create(requestUri);
var response = request.GetResponse();
var xdoc = System.Xml.Linq.XDocument.Load(response.GetResponseStream());
var result = xdoc.Element("GeocodeResponse").Element("result");
var locationElement = result.Element("geometry").Element("location");
var latitude = locationElement.Element("lat");
var longitude = locationElement.Element("lng");
double lat = Double.Parse(latitude.Value);
double lon = Double.Parse(longitude.Value);
var geo = string.Format(CultureInfo.InvariantCulture.NumberFormat, "POINT({0} {1})", lat, lon);
var geoLocation = DbGeography.PointFromText(geo, 4326);
SqlCommand updationCommand = connection.CreateCommand();
updationCommand.CommandText = "Update Communications SET [GeoLocation]=@GeoLoc Where [Id] =@Id";
updationCommand.Parameters.AddWithValue("@Id", row.Id);
updationCommand.Parameters.AddWithValue("@GeoLoc", geoLocation);
var param2 = updationCommand.CreateParameter();
豁免 - "No mapping exists from object type System.Data.Spatial.DbGeography to a known managed provider native type."
使用 SqlGeography 而不是 DbGeography,因为 DbGeography 旨在与 EF 一起使用。
尝试
SqlParameter p = new SqlParameter();
p.Name = "@GeoLoc";
p.Value = SqlGeography.Parse(geoLocation.AsText());
p.SqlDbType = SqlDbType.Udt;
p.UdtTypeName = "geography";
updationCommand.Parameters.Add(p);
更多信息在此 questions