How to create spatial index using EF 6.1 fluent API

后端 未结 2 2061
借酒劲吻你
借酒劲吻你 2021-01-05 02:27

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

2条回答
  •  孤街浪徒
    2021-01-05 03:11

    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.

提交回复
热议问题