1

I am building an app using the DotNetNuke 7 platform and am trying to write Geography data to the database. Here is some background on the project. I am building in VS 2012 and just upgraded to Server 2012 from 2008 R2. DotNetNuke 7 implements PetaPoco for the data layer and WebAPI.

I hope what I provided is enough to information to understand the problem. My code fails on the line "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);
            }
        }
    }
}

Here is my object

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

}

I am passing in the Long and Lat from the client side from a Google map which gets the coords from a mouse click

In my database if I were to directly write an insert or update using the following, it will work.

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

What might be the reason?

-- though I would include a screen shot of the object enter image description here

4

1 に答える 1

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

データベースでは、GeoCode データ型は sql geography 型である必要があります。文字列はこの型に正しく保存されます。その後、GeoCodeObj をクラスで自由に使用できます。私は getter を public のままにし、setter を protected として置きましたが、DAL2 がマッピング クラスに対して持っている制約について確信が持てません。

さらなる調査とフィードバックの後に更新された回答を編集

于 2013-09-23T14:12:47.913 に答える