Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 586
Imantas Avatar asked Jan 30 '19 09:01

Imantas


People also ask

How does spatial indexing work?

A spatial index is a data structure that allows for accessing a spatial object efficiently. It is a common technique used by spatial databases. Without indexing, any search for a feature would require a "sequential scan" of every record in the database, resulting in much longer processing time.

What is spatial index in Oracle?

4.1 Creating a Spatial Index. Once data has been loaded into the spatial tables through either bulk or transactional loading, a spatial index must be created on the tables for efficient access to the data. Each spatial index can be an R-tree index or a quadtree index.

What is spatial index in SQL Server?

SQL Server supports spatial data and spatial indexes. A spatial index is a type of extended index that allows you to index a spatial column. A spatial column is a table column that contains data of a spatial data type, such as geometry or geography.

What is ORM in EF core?

Entity Framework Core is what's known as an Object Relational Mapper (ORM). Created by Microsoft, the library allows developers to work abstractly with their database. The library comes with two distinct parts: the ORM and the CLI tools.


1 Answers

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.

like image 108
codingbadger Avatar answered Sep 19 '22 03:09

codingbadger