1

我正在使用 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<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);

可能是什么原因?

- 虽然我会包括对象的屏幕截图 在此处输入图像描述

4

1 回答 1

1

I am not certain on this, but going by how ORMs work, I would say it is because PetaPoco does not know how to map the DbGeography object to the database. You will have to use the string value to try and represent DBGeography. Try something like this:

[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; }
}

In your database, the GeoCode datatype should be the sql geography type.The string will correctly save to this type. You can then use the GeoCodeObj freely in your classes. I have left the getter public and put the setter as protected, but I am not sure of any constraints DAL2 has with mapping classes.

Edit Updated answer after further research and feedback

于 2013-09-23T14:12:47.913 回答