[英]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。 我烦了。
实体框架支持空间数据的两种主要类型:DbGeometry和DBGeography。 Connector / Net 不支持第二个,因为MySQL服务器没有任何与此类型映射相同的类型。因此所有示例都将使用DbGeometry类型。
也许您可以使用DbGeometry数据类型POINT来保存经度和纬度,然后使用Haversine公式来计算距离。
正如ehe888所指出的,MySQL Connector / Net不支持映射到DbGeography
,但支持映射到DbGeometry
。
如果您只需要存储POINT
( 从6.10版本开始,MySQL Connector / Net支持的唯一几何 ),那么您可以在实体中自己在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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.