2013-02-25 49 views
1

我正在使用DotNetNuke 7平台构建应用程序,并试图将Geography数据写入数据库。以下是该项目的一些背景。我在VS 2012中构建,刚刚从2008 R2升级到Server 2012。 DotNetNuke 7为数据层和WebAPI实现了PetaPoco。不存在从对象类型System.Data.Spatial.DbGeography到已知托管提供程序本机类型的映射

希望我提供的信息足以了解问题。我的代码在行“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; } 
    } 

} 

我在长和纬度通过从一个谷歌地图,从获取鼠标坐标中的客户端点击

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

geography:: STGeomFromText('POINT(-121.527200 45.712113)' , 4326); 

可能是什么原因?

- 尽管我会包括对象 enter image description here

+0

能否请您提供完整的异常详细信息(包括堆栈跟踪)的答案,正如它的toString()方法返回? – 2013-09-26 13:05:32

回答

1

的屏幕截图我不是这个肯定的,但要通过的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; } 
} 

在你的数据库,地理编码数据类型应该是SQL地理type.The字符串将正确保存这种类型。然后你可以在你的类中自由使用GeoCodeObj。我已经公开了getter并将setter设置为protected,但我不确定DAL2对映射类有任何约束。

编辑更新后进一步研究和反馈

+0

我认为你是正确的缺乏映射,但正如op所说,数据库确实理解类型,但使用它自己的“从文本点”类型函数。因此我希望有一种方法可以创建映射,或者以SQL可以接受的方式提供。 – dougajmcdonald 2013-09-23 17:24:27

+0

我的错误,我的意思是DNN不知道在sql中调用“从文本中的点”函数将DbGeography对象映射到sql位置数据类型。我知道实体框架确实知道如何映射它。不幸的是,我自己不知道如何在DNN中做自定义类型,所以如果你找到一种方法来做到这一点,知道:) – 2013-09-24 01:43:55

+0

好的小动作!我也需要使用List 来做到这一点。 – 2015-10-12 04:47:31

相关问题