简体   繁体   中英

Got NotSupportedException when using Dapper to call a PostgreSQL function with a geography argument

I was using Dapper (using npgsql data provider with NetTopologySuite plugin) to call a PostgreSQL function with a geography argument then received a NotSupportedException :

System.NotSupportedException: The member _location of type NetTopologySuite.Geometries.Point cannot be used as a parameter value
at Dapper.SqlMapper.LookupDbType(Type type, String name, Boolean demand, ITypeHandler& handler) in C:\projects\dapper\Dapper\SqlMapper.cs:line 417
at Dapper.SqlMapper.CreateParamInfoGenerator(Identity identity, Boolean checkForDuplicates, Boolean removeUnused, IList`1 literals) in C:\projects\dapper\Dapper\SqlMapper.cs:line 2516
at Dapper.SqlMapper.GetCacheInfo(Identity identity, Object exampleParameters, Boolean addToCache) in C:\projects\dapper\Dapper\SqlMapper.cs:line 1707
at Dapper.SqlMapper.ExecuteScalarImplAsync[T](IDbConnection cnn, CommandDefinition command) in C:\projects\dapper\Dapper\SqlMapper.Async.cs:line 1207
...

But it works fine when I use NpgsqlCommand with type specified via AddWithValue method.

How can I make Dapper map NetTopologySuite.Geometries.Point to geography ?

After doing some search I found an unaccepted answer that solved my problem.

Make sure I added Npgsql.NetTopologySuite package and its mapper is enabled connection.TypeMapper.UseNetTopologySuite(); then the ADO.Net command works fine.

I added a custom Dapper.SqlMapper.TypeHandler :

public class GeographyTypeMapper : SqlMapper.TypeHandler<Geometry> {
    public override void SetValue(IDbDataParameter parameter, Geometry value) {
        if (parameter is NpgsqlParameter npgsqlParameter) {
            npgsqlParameter.NpgsqlDbType = NpgsqlDbType.Geography;
            npgsqlParameter.NpgsqlValue = value;
        } else {
            throw new ArgumentException();
        }
    }

    public override Geometry Parse(object value) {
        if (value is Geometry geometry) {
            return geometry;
        } 

        throw new ArgumentException();
    }
}

Then use it SqlMapper.AddTypeHandler(new GeographyTypeMapper()); and everything works fine.

I'd you just upgraded to Npgsql 4, you will need to uses one of the spatial plugins. Read [the release notes] ( http://www.npgsql.org/doc/release-notes/4.0.html#improved-spatial-support-postgis ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM