从对象类型System.Data.Spatial.DbGeography到已知的托管提供程序本机类型不存在映射

我正在使用DotNetNuke 7平台构建应用程序,并尝试将地理数据写入数据库。 以下是该项目的一些背景知识。 我在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(); 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; } } } 

我从客户端通过Long和Lat从Google地图传递,通过鼠标点击获取坐标

在我的数据库中,如果我使用以下内容直接编写插入或更新,它将起作用。

 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 geography类型。字符串将正确保存为此类型。 然后,您可以在课程中自由使用GeoCodeObj。 我已经将getter公开并将setter保护为受保护,但我不确定DAL2对映射类的任何约束。

编辑在进一步研究和反馈后更新的答案