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?
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.
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.
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.
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.
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
Issue 1100 is support Spatial Data Types on SQL Server and SQL Lite.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With