执行所有 Gets、Puts、Posts 等操作的 SDK 是 PCL。因此,我无法安装实体框架。
使用 Postman 的基本获取返回有关 DbGeography 的以下数据。
"GeoLocation": {
"$id": "6",
"Geography": {
"$id": "7",
"CoordinateSystemId": 4326,
"WellKnownText": "POINT (-98.7687222 32.8494985)"
}
},
由于我无法安装实体框架,因此我尝试创建自己的 DbGeography 和 DbGeographyWellKnownValue。
我从应用程序进行的 API 调用应返回 4 条完整记录。相反,一旦 Json 被反序列化,它就会返回 1 条完整记录和 3 条空记录。我认为这与 DbGeography 字段有关。我试过添加 [JsonIgnore],但这仍然没有得到 4 条完整的记录。这是我的 Location、DbGeography、DbGeographyWellKnownValue 和 DbGeographyConverter 类。
地点
[JsonProperty("GeoLocation")]
[JsonConverter(typeof(DbGeographyConverter))]
public DbGeography GeoLocation { get; set; }
地理数据库
public class DbGeography
{
[System.Runtime.Serialization.DataMemberAttribute(Name = "Geography")]
public DbGeographyWellKnownValue Geography { get; set; }
/// <summary>
/// Custom FromText method
/// </summary>
/// <param name="wellKnownText"></param>
/// <param name="coordinateSystemId"></param>
/// <returns></returns>
public static DbGeography FromText(string wellKnownText, int coordinateSystemId)
{
DbGeography Geography = new DbGeography()
{
Geography = new DbGeographyWellKnownValue()
{
WellKnownText = wellKnownText,
CoordinateSystemId = coordinateSystemId,
}
};
return Geography;
}
}
DbGeography众所周知的值
public class DbGeographyWellKnownValue
{
/// <summary>
/// The coordinate ID
/// </summary>
[System.Runtime.Serialization.DataMemberAttribute(Order = 1, IsRequired = false, EmitDefaultValue = false)]
public int? CoordinateSystemId { get; set; }
/// <summary>
/// Optional.
/// </summary>
//public string WellKnownBinary { get; set; }
/// <summary>
/// The WellKnownText value
/// </summary>
[System.Runtime.Serialization.DataMemberAttribute(Order = 2, IsRequired = false, EmitDefaultValue = false)]
public string WellKnownText { get; set; }
}
最后,我有一个DbGeographyConverter类。
public class DbGeographyConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
//return objectType.IsAssignableFrom(typeof(string));
return objectType.Equals(typeof(DbGeography));
//return objectType.Equals(typeof(string));
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
//Handles issue with null DbGeography values
if (reader.TokenType == JsonToken.Null)
return default(DbGeography);
JObject location = JObject.Load(reader);
JToken token = location["Geography"]["WellKnownText"];
JToken token2 = location["Geography"]["CoordinateSystemId"];
string point = token.ToString();
int coordinateId = Convert.ToInt32(token2.ToString());
//If value is null lets return the default
if (point == null)
return default(DbGeography);
//If we get to here lets convert
var converted = DbGeography.FromText(point, coordinateId);
return converted;
//Can't convert, return NULL
//return default(DbGeography);
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
// Base serialization is fine
serializer.Serialize(writer, value);
//Handle null values
//var dbGeography = value as DbGeography;
//serializer.Serialize(writer, dbGeography == null || dbGeography.IsEmpty ? null : value);
//serializer.Serialize(writer, dbGeography == null ? null : value);
}
}
这一切都在我的 MVC 5 Web 应用程序中完美运行,并且使用相同的 API,所以我知道它与 DbGeography 字段有关。
任何帮助深表感谢。谢谢!