OData + EF. Writing geography types

别来无恙 提交于 2019-12-06 15:52:42

I've had this problem too. Its a bug apparently.

Either two choices here - drop down to EF5 for the old System.Data.Entity.DbGeography (rather than System.Data.Entity.Spatial.DbGeography), or wait until they patch it.

Edit: Its been a long time, and so far the only hack I've come up with is this as a way to write to the properties and hide the ugly serialised format (however still cannot query against it.

    private bool hasSetLongitude = false;
    private bool hasSetLatitiude = false;
    private double? longitude = null;
    private double? latitiude = null;

    /// <summary>
    /// Get or set the coordinates.
    /// </summary>
    [Column("engi_coord"), Required]
    public DbGeography Coordinates { get; set; }

    /// <summary>
    /// Get or set the lang coordinates.
    /// </summary>
    [Column("engi_lng"), DatabaseGenerated(DatabaseGeneratedOption.Computed)]
    public double? Longitude
    {
        get 
        { 
            return this.Coordinates != null ? this.Coordinates.Longitude : (double?)null; 
        }
        set 
        {
            this.longitude = value;
            this.hasSetLongitude = true;
            if (this.hasSetLongitude)
            {
                if (this.longitude.HasValue &&
                    this.latitiude.HasValue)
                {
                    this.Coordinates = DbGeography.PointFromText(string.Format("POINT({0} {1})", this.longitude.Value, this.latitiude.Value), 4326);
                }
                else
                {
                    this.Coordinates = null;
                }
            }
        }
    }

    /// <summary>
    /// Get or set the lat coordinates.
    /// </summary>
    [Column("engi_lat"), DatabaseGenerated(DatabaseGeneratedOption.Computed)]
    public double? Latitude
    {
        get 
        { 
            return this.Coordinates != null ? this.Coordinates.Latitude : (double?)null; 
        }
        set 
        {
            this.latitiude = value;
            this.hasSetLatitiude = true;
            if (this.hasSetLatitiude)
            {
                if (this.longitude.HasValue &&
                    this.latitiude.HasValue)
                {
                    this.Coordinates = DbGeography.PointFromText(string.Format("POINT({0} {1})", this.longitude.Value, this.latitiude.Value), 4326);
                }
                else
                {
                    this.Coordinates = null;
                }
            }
        }
    }

Migrate to add the computed columns:

        this.Sql("ALTER TABLE dbo.engi_engineer ADD engi_lng AS engi_coord.Long");
        this.Sql("ALTER TABLE dbo.engi_engineer ADD engi_lat AS engi_coord.Lat");

Now ignore the coordinates property from API:

        this.EntityType.Ignore(p => p.Coordinates);

Well it has been over a year now and it doesn't look like this is getting fixed anytime soon. Especially with EF7 coming out very soon.

So, I would like to approach this another way.
What if the DbGeography type was not exposed as a property on the class and instead we expose a Latitude and Longitude property that are not mapped and then on their GET / SET we update the hidden DbGeography property?

I have tried this but have been unsuccessful with EF.
What you can do is inside the POST / PUT controller calls is use a stored procedure or SQL text to update the data.

If I find out how to get this to work with EF I will post here.

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