Is it possible to use a NetTopologySuite Point in a uniqueness key constraint using entity framework?

258 views Asked by At

I'm building an API using .net core and entity framework core with NetTopologySuite.Core and Microsoft.EntityFrameworkCore.SqlServer.NetTopologySuite.

I have a use case where I want to store a set of geo locations that together builds up a net. I want to have multiple nets, so I have also added a NetId representing the different nets.

The Location property is of type NetTopologySuite.Geometries.Point.

To make sure the same Point do not get added two times for the same net, I would like to use the combination of Point and NetId as a unique key constraint.

I have tried to do it like this:

modelBuilder.Entity<NetLocation>().HasIndex(m => new { m.NetId, m.Point}).IsUnique();

When starting my application I get this error:

Column 'Location' in table 'MyTable' is of a type that is invalid for use as a key column in an index or statistics.

How can I make sure that the exact same location not get added twice to the same net?

1

There are 1 answers

0
joakimriedel On

With SQL Server it's possible to add computed columns, PointLat = "Point.Lat" and PointLong = "Point.Long", and then use them in the index

modelBuilder.Entity<NetLocation>().HasIndex(m => new { m.NetId, m.PointLat, m.PointLong }).IsUnique();

Should work, but untested. Don't know about other providers, but they probably have similar geography support.