我找到了一些相关的 questions , 但作者放弃了,继续使用存储过程来进行“映射”。
这实际上是 here 的延续问题
型号
public class Store
{
public int Id { get; private set; }
public string Name { get; set; }
public string Address { get; set; }
public DbGeography Location { get; set; }
}
查询
using (SqlConnection conn = SqlHelper.GetOpenConnection())
{
const string sql = "Select * from Stores";
return conn.Query<Store>(sql, new { Tenant_Id = tenantId });
}
Dapper 不理解空间数据,正如许多人所说,支持供应商特定实现并不是作者的初衷。但是文档扩展Query<T>
很难找到支持
最佳答案
我对此有一个探索 here ,以下测试通过:
class HazGeo
{
public int Id { get;set; }
public DbGeography Geo { get; set; }
}
public void DBGeography_SO24405645_SO24402424()
{
global::Dapper.SqlMapper.AddTypeHandler(typeof(DbGeography), new GeographyMapper());
connection.Execute("create table #Geo (id int, geo geography)");
var obj = new HazGeo
{
Id = 1,
Geo = DbGeography.LineFromText("LINESTRING(-122.360 47.656, -122.343 47.656 )", 4326)
};
connection.Execute("insert #Geo(id, geo) values (@Id, @Geo)", obj);
var row = connection.Query<HazGeo>("select * from #Geo where id=1").SingleOrDefault();
row.IsNotNull();
row.Id.IsEqualTo(1);
row.Geo.IsNotNull();
}
class GeographyMapper : Dapper.SqlMapper.TypeHandler<DbGeography>
{
public override void SetValue(IDbDataParameter parameter, DbGeography value)
{
parameter.Value = value == null ? (object)DBNull.Value : (object)SqlGeography.Parse(value.AsText());
((SqlParameter)parameter).UdtTypeName = "GEOGRAPHY";
}
public override DbGeography Parse(object value)
{
return (value == null || value is DBNull) ? null : DbGeography.FromText(value.ToString());
}
}
它看起来可行,但我还没有点缀每个 i 并穿过每个 t。欢迎您在本地试验该提交 - 我很乐意提供反馈。
关于c# - 使用 dapper 查询空间数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24405645/