Post DbGeography data type in odata v4 api

一世执手 提交于 2021-02-19 06:18:07

问题


What i receive from a DbGeography data type is in Location.

{
    "@odata.context":"http://subdomain.mydomain.me/odata/$metadata#Pois","value":[
      {
          "Id":"d57e6603-5530-4457-b041-6398b7182173","Name":"Demo Home","Address":"Zuidstraat 8 8630 Veurne","Location":{
              "Geography":{
                  "CoordinateSystemId":4326,"WellKnownText":"POINT (51.515574 2.025285)","WellKnownBinary":null
              }
          },"TagId":"ec32e8f3-c0b8-4bcc-af6c-7059b1ef1a65","RouteId":null
      }
    ]
}

I have tried multiple POSTS, but none of them seem to succeed in adding a DbGeography Point.

Any thoughts on how to do this? I have tried adding the same as what is returned ( doesn't work) and using type + coordinates as properties ( as defined in the odata standard .

In another question, i have found this Error getting value from 'WellKnownValue' on 'System.Data.Entity.Spatial.DbGeography , but this solution seems insufficent.

If i'd add Location : "POINT(lat long)" i could use the following method to generate the data ( but i don't know how)

DbGeography.PointFromText(textValue, 4326)

回答1:


I send the coordinates as longitude, latitude then converts to DbGeography at server side like this:

Model

public class MyModel
{
    [Required]
    public  double Longitude { get; set; }
    [Required]
    public double Latitude { get; set; }
}

Controller

public IHttpActionResult Post(MyModel model)
{
    if (!ModelState.IsValid || model == null)
    {
        // return error
    }

    var coordinates = DbGeography.FromText($"POINT({model.Longitude} {model.Latitude})");

    // save coordinates to database

    return Ok();
}


来源:https://stackoverflow.com/questions/33792793/post-dbgeography-data-type-in-odata-v4-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!