Is there a way to declare a Spatial Index with EntityFrameworkCore 2.2?

c# entity-framework-core nettopologysuite sqlgeography sql-server

Question

I am using Entity Framework Core 2.2 with NetTopologySuite 1.15.1 and SQL Server 2016. Having a column of type IPoint works great to the point I want to create an index on it.

I have this table

public class Location
{
    public int Id { get; set; }

    public IPoint Coordinates { get; set; }

    public static void Register(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Location>(entity => entity.HasIndex(x => new {x.Coordinates}));
    }
}

And EF core handles it well while generating a migration thus producing the following code:

migrationBuilder.CreateIndex(
    name: "IX_Locations_Coordinates",
    schema: "data",
    table: "Locations",
    column: "Coordinates");

However, as soon as I try to apply the migration to database, SqlException is thrown with the following message

SqlException: Column 'Coordinates' in table 'data.Locations' is of a type that is invalid for use as a key column in an index or statistics.

SQL Server does support indexes on columns of type geography.

I believe the reason might be that EF Core is trying to create a regular index rather than spatial one.

Am I doing something wrong or it's just not supported? Is there a way to tell EF Core that it should create spatial index?

1
7
1/30/2019 9:00:17 AM

Accepted Answer

Looking at the issues log for EF Core it appears that creating Spatial Indexes isn't yet supported.

https://github.com/aspnet/EntityFrameworkCore/issues/12538

github

Issue 1100 is support Spatial Data Types on SQL Server and SQL Lite.

7
1/30/2019 9:27:49 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow