【发布时间】:2014-06-04 06:47:30
【问题描述】:
我的 SQL 数据库中有带有 sql 地理列的表。我已经使用 EF6 为我的数据库生成了实体。如您所知,Entity Framework 为 SQL 地理类型生成 System.Data.Entity.Spatial.DbGeography。我正在使用 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();
【问题讨论】:
-
将您的作品显示为文本,而不是图像..
标签: c# sql entity-framework dapper sqlgeography