2

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?

David Berg
  • 1,958
  • 1
  • 21
  • 37
  • Why don't you do a check, if a Point exists for a given NetId and than make the insert? – TheMixy Nov 23 '20 at 18:23
  • 1
    Such Point is mapped to the special spatial column type (geography) on sql server, and only special spatial index can be created on it. As far as I know, EF doesn't support spatial indexes (their creation has different syntax from regular indexes), and also - you cannot create spatial index on multiple columns anyway (not to mention one of those columns is not spatial). – Evk Nov 23 '20 at 19:43

1 Answers1

0

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.

joakimriedel
  • 1,801
  • 12
  • 27