我正在按照John Papa在他最新的PluralSight课程中概述的方法,使用Angular,Breeze和Web API 2构建SPA.
一切顺利,我可以将信息,更新,插入,删除回服务器.但是我使用的是空间类型,当我尝试更新具有空间类型的实体时,会得到以下错误
An exception of type ‘Newtonsoft.Json.JsonSerializationException’ occurred in
Newtonsoft.Json.dll but was not handled in user codeAdditional information: Error getting value from ‘WellKNownValue’ on
‘System.Data.Entity.Spatial.DbGeometry’.
内部异常似乎指出,WellKNownValue为null,它不是,因为我已经检查发送到服务器的JSON,然后发送到Breeze ContextProvider并使用SaveChanges方法保存Well事实.
{ "entities": [ { "TableKey": 2,"CaseName": "Mikhail Lermontov","StartDate": "2013-06-11T00:00:00Z","EndDate": null,"IsCurrent": true,"SRID": 109,"Shape": { "$id": "2","$type": "System.Data.Entity.Spatial.DbGeometry,EntityFramework","Geometry": { "$id": "3","$type": "System.Data.Entity.Spatial.DbGeometryWellKNownValue,"CoordinateSystemId": 2193,"WellKNownText": "polyGON ((1695943 5462665,1713098 5462665,1713098 5449659,1695943 5449659,1695943 5462665))" } },"SpillLocation": "Marlborough Sounds","Image": "http://www.nzmaritime.co.nz/images/lm5.jpg\r\n","DefaultBaseMapKey": 2,"__unmapped": { "isPartial": false },"entityAspect": { "entityTypeName": "DatSpillCase:#Osiris.Model","defaultResourceName": "DatSpillCases","entityState": "Modified","originalValuesMap": { "CaseName": "Mikhail Lermontov" },"autoGeneratedKey": { "propertyName": "TableKey","autoGeneratedKeyType": "Identity" } } } ],"saveOptions": {} }
所以我的问题是,可以在NewtonSoft库中反序列化DbGeometry类型,如果没有,那么有什么建议可以解决.
解决方法
System.Data.Spatial.DbGeometry不能很好地与Newtonsoft.Json
您需要创建一个JsonConverter来转换DbGeometry
public class DbGeometryConverter : JsonConverter { public override bool CanConvert(Type objectType) { return objectType.IsAssignableFrom(typeof(string)); } public override object ReadJson(JsonReader reader,Type objectType,object existingValue,JsonSerializer serializer) { JObject location = JObject.Load(reader); JToken token = location["Geometry"]["WellKNownText"]; string value = token.ToString(); DbGeometry converted = DbGeometry.polygonFromText(value,2193); return converted; } public override void WriteJson(JsonWriter writer,object value,JsonSerializer serializer) { // Base serialization is fine serializer.Serialize(writer,value); } }
[JsonConverter(typeof(DbGeometryConverter))] public DbGeometry Shape { get; set; }
现在,当您点击BreezeController时,反序列化将由我们的新DbGeometryConverter处理.
希望它有帮助.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。