npgsql / efcore.pg

Entity Framework Core provider for PostgreSQL
PostgreSQL License
1.54k stars 226 forks source link

Using a CLR type derived from a NetTopologySuite type #1784

Closed john-larson closed 3 years ago

john-larson commented 3 years ago

I would like to express lat and long as "latitude" and "longitude" in code instead of X and Y which is what NTS supports. How would it be possible to use the following type instead of NetTopologySuite Point:

    public class TopologyPoint: Point
    {
        public TopologyPoint(double latitude, double longitude) : base(latitude, longitude) { }

        public TopologyPoint(double latitude, double longitude, double altitude) : base(latitude, longitude, altitude) { }

        public double Latitude => Y;

        public double Longitude => X;

    }

When I try to use it, I get the following error for insert:

InvalidCastException: Can't write CLR type TopologyPoint with handler type NetTopologySuiteHandler

roji commented 3 years ago

@john-larson you can't extend and map arbitrary types in this way - the EF NetTopologySuite provider knows about Point, but not about your specific type.

If all you want to do is to provide Latitude/Longtitude aliases, you can add extension methods. Another possibility would be to use EF Core value converters to convert from your custom type to the standard Point. With both these techniques, though, you will not be able to use the members in a LINQ query and have that translated to SQL.