从对象类型System.Data.Spatial.DbGeography到已知的托管提供程序本机类型不存在映射

[英]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.

我正在使用DotNetNuke 7平台构建应用程序,并尝试将地理数据写入数据库。以下是该项目的一些背景知识。我在VS 2012中构建,刚刚从2008 R2升级到Server 2012。 DotNetNuke 7为数据层和WebAPI实现了PetaPoco。

I hope what I provided is enough to information to understand the problem. My code fails on the line "rep.Insert(location);"

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

我从客户端通过Long和Lat从Google地图传递,通过鼠标点击获取坐标

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

- 虽然我会包括对象的屏幕截图

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:

我不确定这一点,但是按照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; }
}

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.

在您的数据库中,GeoCode数据类型应该是sql geography类型。字符串将正确保存为此类型。然后,您可以在课程中自由使用GeoCodeObj。我已经将getter公开并将setter保护为受保护,但我不确定DAL2对映射类的任何约束。

Edit Updated answer after further research and feedback

编辑在进一步研究和反馈后更新的答案


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.silva-art.net/blog/2013/02/25/6f8317035530659b39c32e7bcf46c4df.html



 
© 2014-2019 ITdaan.com 粤ICP备14056181号