I am trying to get the Polygon as MySqlGeometry using Dapper.
But the MySql connector understands only MySqlGeometry as Point.
And I need the other types also, like Polygon.
This is what I got from other forums:
// 1. Add Dapper custom Type handler.
Dapper.SqlMapper.AddTypeHandler(new MySqlGeometryTypeHandler());
// 2. Implement that custome handler.
public class MySqlGeometryTypeHandler : SqlMapper.TypeHandler<MySqlGeometry>
{
public override MySqlGeometry Parse(object value) { return new MySqlGeometry(MySqlDbType.Geometry, (byte[])value); }
public override void SetValue(System.Data.IDbDataParameter parameter, MySqlGeometry value) { parameter.Value = value; }
}
// 3. Here is the Data class
public class Geo
{
public int Id { get; set; }
public MySqlGeometry G { get; set; }
}
// 4. Here is the Dapper query
List<Geo> datas = Dapper.SqlMapper.Query<Geo>(cnn, "select * from geo;").ToList();
How do I get the Polygon rows I have in the 'geo' table ?
It comes out - MySqlGeometry doesn't support (hopefully yet) other types than Point.
So the solution I got is:
public class Geo
{
public int Id { get; set; }
public DbGeography G { get; set; }
}
public class DbGeographyTypeHandler : SqlMapper.TypeHandler<DbGeography>
{
public override DbGeography Parse(object value) { return DbGeography.FromBinary((byte[])value); }
public override void SetValue(IDbDataParameter parameter, DbGeography value) { parameter.Value = value.AsBinary(); }
}
Dapper.SqlMapper.AddTypeHandler<DbGeography>(new DbGeographyTypeHandler());
List<Geo> datas = Dapper.SqlMapper.Query<Geo>(conn, "select id, st_aswkb(g) g from geo;").ToList();