[英]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.