我的 SQL 数据库中有带有 sql geography 列的表。我已经使用 EF6 为我的数据库生成了实体。如您所知,实体框架System.Data.Entity.Spatial.DbGeography
为 SQL 地理类型生成。我正在使用 dapper 运行查询并将结果映射到我的 EF 生成的实体中。
我的实体类
public partial class Fix
{
public Fix()
{
this.FixUsers = new HashSet<FixUser>();
}
public long FixID { get; set; }
public long UserID { get; set; }
public System.Data.Entity.Spatial.DbGeography Position { get; set; }
public int Radius { get; set; }
public System.DateTime CreatedDate { get; set; }
public virtual MemberProfile MemberProfile { get; set; }
public virtual ICollection<FixUser> FixUsers { get; set; }
}
引发异常的 SQL 查询
var fix = SqlConnection.Query<Fix>(@"SELECT TOP(1)
f.FixID as FixID,
f.UserID as UserID,
f.Radius as Radius,
f.CreatedDate as CreatedDate,
f.Position as Position
FROM [Fix] f
WHERE f.FixID = @fixId", new { fixId }).FirstOrDefault();
这是异常快照
我认为默认情况下,dapper 正在尝试映射到Microsoft.SqlServer.Types.SqlGeography
.
这里有什么解决方法吗?
已编辑
找到了一些解决方案,为我的实体创建了部分类
public partial class Fix
{
public string PositionString
{
set
{
Position = DbGeography.PointFromText(value, 4326);
}
}
}
并改变了我的查询
var fix = SqlConnection.Query<Fix>(@"SELECT TOP(1)
f.FixID as FixID,
f.UserID as UserID,
f.Radius as Radius,
f.CreatedDate as CreatedDate,
f.Position.ToString() as PositionString
FROM [Fix] f
WHERE f.FixID = @fixId", new { fixId }).FirstOrDefault();