簡體   English   中英

使用MySQL和EntityFramework的DbGeography

[英]DbGeography with MySQL and EntityFramework

我正在使用System.Data.Entity.Spatial; DbGeography System.Data.Entity.Spatial; 和SQL Server數據庫的實體框架。 現在我切換到使用MySQL服務器,當我創建與DbGeography類型相關的數據庫時,我收到一個錯誤。

如何在不將所有域類屬性更改為其他類型的情況下解決此問題?

public class Place
{
  ...
    public virtual int Id { get; set; }
    public string Name { get; set; }
    public DbGeography Location {get;set;}
  ...

}

我遇到的錯誤就是這個錯誤:

System.NotSupportedException: There is no store type corresponding to the EDM type 'Edm.Geography' of primitive type 'Geography'.

我已經瀏覽了一堆MySQL文檔,發現mysql 5.x目前不支持DbGeography數據類型。

對於mysql 5.x,僅支持DbGeometry。 我煩了。

請參閱有關空間數據支持的MySQL官方文檔文檔

實體框架支持空間數據的兩種主要類型:DbGeometry和DBGeography。 Connector / Net 支持第二個,因為MySQL服務器沒有任何與此類型映射相同的類型。因此所有示例都將使用DbGeometry類型。

也許您可以使用DbGeometry數據類型POINT來保存經度和緯度,然后使用Haversine公式來計算距離。

正如ehe888所指出的,MySQL Connector / Net不支持映射到DbGeography ,但支持映射到DbGeometry

如果您只需要存儲POINT從6.10版本開始,MySQL Connector / Net支持的唯一幾何 ),那么您可以在實體中自己在DbGeographyDbGeometry之間輕松進行轉換:

    [Column("gps_location")]
    public DbGeometry LocationGeometry { get; set; }

    [NotMapped] // Mapping to DbGeography is not supported by MySQL Connector/Net, see https://dev.mysql.com/doc/connector-net/en/connector-net-entityframework50.html
    public DbGeography Location
    {
        get => LocationGeometry != null ? DbGeography.FromBinary(LocationGeometry.AsBinary()) : null;
        set => LocationGeometry = value != null ? DbGeometry.FromBinary(value.AsBinary()) : null;
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM