我正在使用来自DbGeography的System.Data.Entity.Spatial;和实体框架与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'.发布于 2015-06-22 16:49:22
我浏览了大量MySQL文档,发现MySQL5.x目前不支持DbGeography数据类型。
对于MySQL5.x,只支持DbGeometry。我很沮丧。
请参阅文档关于空间数据支持的MySQL正式文档
实体框架支持空间数据的两种主要类型: DbGeometry和DBGeography。第二个是连接器/网不支持的,因为MySQL服务器没有映射此类型的任何等效类型。因此,所有示例都将使用DbGeometry类型。
也许您可以使用DbGeometry数据类型点来保存经度和纬度,然后使用Haversine公式计算距离。
发布于 2018-03-20 08:11:42
正如ehe888所指出的,MySQL连接器/网不支持到DbGeography的映射,而是支持到DbGeometry的映射。
如果您只需要存储一个POINT (MySQL连接器/网在6.10版时支持的唯一几何图形),那么您可以轻松地在实体中自己完成DbGeography和DbGeometry之间的转换:
[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;
}https://stackoverflow.com/questions/25046665
复制相似问题