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

j0k
  • 22,600
  • 28
  • 79
  • 90
Mark Hollas
  • 1,107
  • 1
  • 16
  • 44

1 Answers1

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

Melvin DVaz
  • 1,234
  • 6
  • 5
  • I think you're right in the lack of mapping, but as the op says, the DB does understand the type, but uses it own 'point from text' type function. As a result I was hoping there would be a way to create the mapping, or provide it in a way that SQL would accept. – dougajmcdonald Sep 23 '13 at 17:24
  • My mistake, I meant that DNN doesnt know to call the 'point from text' function in sql to map a DbGeography object to an sql Location data type. I know that Entity Framework does know how to map this though. Unfortunately I myself don't know how to do custom types in DNN, so if you find a way to do such, it would be interesting to know :) – Melvin DVaz Sep 24 '13 at 01:43
  • Nice little trick! I needed to do this with a List too. – Will Strohl Oct 12 '15 at 04:47