没有映射从对象类型System.Data.Spatial.DbGeography存在一个已知的托管提供原生型 [英] No mapping exists from object type System.Data.Spatial.DbGeography to a known managed provider native type

查看:362
本文介绍了没有映射从对象类型System.Data.Spatial.DbGeography存在一个已知的托管提供原生型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建立使用DotNetNuke的7平台的应用程序,我试图地理数据写入到数据库中。下面是该项目的一些背景知识。我建设VS 2012和2008 R2刚刚升级到Server 2012中。 DotNetNuke的7实现PetaPoco的数据层和的WebAPI。

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.

我希望我提供足够的信息来理解这个问题。我的code上线失败rep.Insert(位置);

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

下面是我的目标。

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

}

我传递的长和纬度从客户端从谷歌地图从鼠标点击获取COORDS

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

推荐答案

我不是这个肯定的,但奥姆斯是如何工作的打算,我会说这是因为PetaPoco不知道如何DbGeography对象映射到数据库。你将不得不使用字符串值,试图重新present DBGeography。尝试是这样的:

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

在你的数据库中,地理code的数据类型应该是SQL地理type.The字符串将正确保存这种类型。然后,您可以在你的类使用地理codeObj自如。我已经离开了公众的getter,把二传手作为保护,但我不知道的任何约束DAL2与映射类。

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

这篇关于没有映射从对象类型System.Data.Spatial.DbGeography存在一个已知的托管提供原生型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆