[英]Exception : No mapping exists from object type System.Data.Spatial.DbGeography to a known managed provider native type
[英]No mapping exists from object type System.Data.Spatial.DbGeography to a known managed provider native type
我正在使用DotNetNuke 7平台構建應用程序,並試圖將Geography數據寫入數據庫。 這是該項目的一些背景。 我在VS 2012中構建,並且剛剛從2008 R2升級到Server 2012。 DotNetNuke 7為數據層和WebAPI實現了PetaPoco。
我希望我所提供的信息足以理解問題。 我的代碼在“ rep.Insert(location);”行上失敗。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net;
using System.Web;
using System.Net.Http;
using System.Web.Http;
using System.Data.Spatial;
using DotNetNuke.Web.Api;
using DotNetNuke.Data;
namespace DotNetNuke.Modules.GeoLocations
{
public class LocationController: DnnApiController
{
[AllowAnonymous]
[HttpPost]
[ValidateAntiForgeryToken]
public HttpResponseMessage addLocation(CP_Location submitted)
{
submitted.GeoCode = DbGeography.PointFromText(string.Format("POINT({0} {1})", submitted.Long, submitted.Lat), 4326);
createLocation(submitted);
return Request.CreateResponse(HttpStatusCode.OK, "Success");
}
//------------------------------CRUD------------------------------//
public void createLocation(CP_Location location)
{
using (IDataContext ctx = DataContext.Instance())
{
var rep = ctx.GetRepository<CP_Location>();
rep.Insert(location);
}
}
}
}
這是我的對象
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Spatial;
using System.Data.Entity;
using DotNetNuke.ComponentModel.DataAnnotations;
using DotNetNuke.Data;
using DotNetNuke.Data.PetaPoco;
namespace DotNetNuke.Modules.GeoLocations
{
[TableName("CP_Locations")]
[PrimaryKey("LocationId", AutoIncrement = true)]
public class CP_Location
{
public int LocationId { get; set; }
public string LocationType { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public float Long { get; set; }
public float Lat { get; set; }
public DbGeography GeoCode { get; set; }
}
}
我正在從客戶端通過Google地圖傳遞Long and Lat,該地圖通過鼠標單擊獲得坐標
在我的數據庫中,如果我要使用以下內容直接編寫插入或更新,則它將起作用。
geography:: STGeomFromText('POINT(-121.527200 45.712113)' , 4326);
可能是什么原因?
-盡管我會包括物體的屏幕截圖
我不確定,但是按照ORM的工作方式,我想說這是因為PetaPoco不知道如何將DbGeography對象映射到數據庫。 您將必須使用字符串值來嘗試表示DBGeography。 嘗試這樣的事情:
[TableName("CP_Locations")]
[PrimaryKey("LocationId", AutoIncrement = true)]
public class CP_Location
{
public int LocationId { get; set; }
public string LocationType { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public float Long { get; set; }
public float Lat { get; set; }
[IgnoreColumn]
public DbGeography GeoCodeObj {
get { return DbGeography.FromText(GeoCode); }
set { GeoCode = value.AsText(); }
}
public string GeoCode { get; protected set; }
}
在您的數據庫中,GeoCode數據類型應為sql地理類型。字符串將正確保存為該類型。 然后,您可以在課程中自由使用GeoCodeObj。 我已經將getter公開,並將setter設置為受保護的,但是我不確定DAL2對映射類有什么約束。
經過進一步研究和反饋后, 編輯更新后的答案
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.