本文介绍了错误从“WellKnownValue'上'System.Data.Entity.Spatial.DbGeography获得价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是SinglePageApplication内DbGeography型(带风和角)。现在用DbGeography型使用数据时(只读)是没有问题的。

I am using the DbGeography-Type within a SinglePageApplication (with breeze and angular). Now when using the Data with the DbGeography-Type (readOnly) there is no problem.

当我保存它具有DbGeography类型属性的实体出现以下错误:

As soon as I save an entity which has a property of DbGeography-Type I get the following error:

Error getting value from 'WellKnownValue' on 'System.Data.Entity.Spatial.DbGeography'

当数据被序列化到JSON(与newtonsoft JSON.NET还是ODATA /的WebAPI?),该DbGeography得到正确的序列,但属性WellKownValue被称为地理。这也反映在MSDN-文档中:

When the data is serialized to JSON (with newtonsoft JSON.NET or is it ODATA/WebAPI?), the DbGeography gets serialized correctly but the property "WellKownValue" is called "Geography". This is also reflected in the MSDN-Documentation:

https://msdn.microsoft.com/en-us/library/system.data.spatial.dbgeography.wellknownvalue(v=vs.110).aspx

[DataMemberAttribute(Name = "Geography")]
public DbGeographyWellKnownValue WellKnownValue { get; set; }

我的实体看起来像这样(通过网线):

My entity looks like this (over the wire):

{
    ..
    "Name" : "Test",
    "Coordinate" : {
        "$id" : "3",
        "$type" : "System.Data.Entity.Spatial.DbGeography, EntityFramework",
        "Geography" : {
            "$id" : "4",
            "$type" : "System.Data.Entity.Spatial.DbGeographyWellKnownValue, EntityFramework",
            "CoordinateSystemId" : 4326,
            "WellKnownText" : "POINT (8.73275400148029 47.5006958431132)"
        }
    }
}

我想,当它进行反序列化在稍后一点,JSON.NET不知道地理位置我的对象的,物业是实际上可以叫做 WellKnownValue

I guess when it is deserialized at a later point, JSON.NET doesn't know that the Geography-Property of my object is acutally called WellKnownValue.

我现在用的Newtonsoft.Json-包7.0.1版和Microsoft.Data.OData版本5.6.4。

I am using the Newtonsoft.Json-Package version 7.0.1 and Microsoft.Data.OData version 5.6.4.

这怎么解决?

推荐答案

我发现了(与.net反射)的DbGeography类型可以通过默认的构造函数来实例化(不带参数)的可是这个默认构造函数不设置一些重要的私有成员(如_spatialProvider)。

I found out (with .NET Reflector) that the DbGeography-Type can be instantiated by a default constructor (with no arguments) BUT this default constructor doesn't set some important private members (like _spatialProvider).

这导致的NullReferenceException WellKnownValue -Getter反序列化过程调用。所以,我所做的是很多人之前必须做(我希望没有这样做) - 我创建一个自定义的 JsonConverter

This results in a NullReferenceException when WellKnownValue-Getter is called during deserialization. So what I did is what many people before had to do (and I hoped didn't have to do) - I created a custom JsonConverter.

一专长是,我必须在注册它的 BreezeWebApiConfig ,而不是普通的 WebApiConfig

One speciality was, that I had to register it in BreezeWebApiConfig instead of the normal WebApiConfig:

public class MyBreezeConfig : Breeze.ContextProvider.BreezeConfig
{
    protected override JsonSerializerSettings CreateJsonSerializerSettings()
    {
        JsonSerializerSettings result = base.CreateJsonSerializerSettings();
        result.Converters.Add(new WebDbGeographyJsonConverter());
        return result;
    }
}

和转换器:

public class WebDbGeographyJsonConverter : JsonConverter {
    public override bool CanConvert(Type objectType) {
        return typeof(DbGeography).IsAssignableFrom(objectType);
    }

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) {
        var jObj = JObject.Load(reader);
        if (jObj != null) {

            var geography = jObj[WebDbGeography.PROP_WELLKNOWNVALUE];
            if (geography != null) {
                var wktText = geography[WebDbGeography.PROP_WELLKNOWNTEXT].Value<string>();
                if (!string.IsNullOrEmpty(wktText)) {
                    var coordSysId = geography[WebDbGeography.PROP_COORDSYSID].Value<int?>() ?? DbGeography.DefaultCoordinateSystemId;
                    return DbGeography.FromText(wktText, coordSysId);
                }
            }
        }
        return null;
    }

    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) {
        throw new NotImplementedException();
    }

    public override bool CanWrite {
        get {
            // use default implementation of Serialization
            return false;
        }
    }
}

这篇关于错误从“WellKnownValue'上'System.Data.Entity.Spatial.DbGeography获得价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-31 03:43