简体   繁体   中英

No mapping exists from object type System.Data.Spatial.DbGeography to a known managed provider native type

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 在此处输入图片说明

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM