Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create spatial index using EF 6.1 fluent API

Well, the question is clear enough. Is it possible to create spatial indexes using Entity Framework 6.1 fluent API?

like image 698
amiry jd Avatar asked Jul 15 '14 05:07

amiry jd


People also ask

Does fluent API have index?

The Entity Framework Core Fluent API HasIndex method is used to create a database index on the column mapped to the specified entity property. By default, indexes are created for foreign keys and alternate keys.

What does create spatial index do?

The Create Spatial Index Tool within QGIS enables you to create spatial indexes very quickly for your GIS Layers. A spatial index can be very important on a GIS layer, especially for large datasets, as they will allow queries to run a lot quicker and speed up map rendering.

Which method is used to apply configuration to entities or their properties in the Entity Framework Core?

Property Mapping. The Property method is used to configure attributes for each property belonging to an entity or complex type.

Does EF core support fluent API?

EF Fluent API is based on a Fluent API design pattern (a.k.a Fluent Interface) where the result is formulated by method chaining. In Entity Framework Core, the ModelBuilder class acts as a Fluent API.


2 Answers

The only way I know to do this is through a "custom" migration. In EF6, I add a migration (in the example below it's named "V1"), resulting in an new migration with empty Up() and Down() methods. You can then add custom SQL commands to these methods before running update-database to put these in the "normal" migrations flow.

It's possible to modify an existing migration to add these features, but I prefer in practice to keep my automatically scaffolded migrations separate from my customized ones.

public partial class V1 : DbMigration
{
    public override void Up()
    {
        Sql("CREATE SPATIAL INDEX [IX_UserProfileAddresses_Location] ON [dbo].[UserProfileAddresses](Location)");
    }

    public override void Down()
    {
        Sql("DROP INDEX [IX_UserProfileAddresses_Location] ON [dbo].[UserProfileAddresses]");
    }
}

Not an ideal method, but not too bad since it does follow the "normal" migrations pattern for EF.

like image 108
R. Lennox Avatar answered Oct 13 '22 06:10

R. Lennox


Short answer- No, it is not. I have seen this tangentially referenced throughout blogs and have found no concrete examples of implementation. It seems to be related to the fact that spatial indexes are filtered indexes, which are not supported in Entity Framework.

As support for my answer I constructed a POC console app with the most recent version of Entity Framework (6.1). I took the following steps

  1. Created a model that had a property of the type DbGeography
  2. Enabled automatic migrations
  3. Ran Update-Database -verbose insuring migration with the addition of an index was run. The index used the following:

    modelBuilder.Entity<LocationEntity>().Property(t => t.Coordinates).HasColumnAnnotation("Index", new IndexAnnotation(new IndexAttribute("ix_locationentity_coordinates")));

No indexes were created, but neither did the app crash. I could try permutations on this, but my example seems to follow the convention of entity framework: Official Fluent Documentation

like image 29
Craig Avatar answered Oct 13 '22 05:10

Craig