jav*_*iry 8 entity-framework spatial spatial-index entity-framework-6.1 ef-fluent-api
嗯,这个问题很清楚.是否可以使用Entity Framework 6.1流畅的API 创建空间索引?
小智 7
我知道这样做的唯一方法是通过“自定义”迁移。在 EF6 中,我添加了一个迁移(在下面的示例中它被命名为“V1”),从而产生一个带有空 Up() 和 Down() 方法的新迁移。然后,您可以在运行 update-database 之前将自定义 SQL 命令添加到这些方法中,以将它们放入“正常”迁移流程中。
可以修改现有迁移以添加这些功能,但在实践中我更喜欢将我的自动脚手架迁移与我的自定义迁移分开。
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]");
}
}
Run Code Online (Sandbox Code Playgroud)
不是一种理想的方法,但也不错,因为它确实遵循 EF 的“正常”迁移模式。
简短的回答-不,不是。我在博客中看到了这一点,但没有找到具体的实现示例。这似乎与空间索引是过滤索引有关,实体框架不支持过滤索引。
作为对我答案的支持,我使用最新版本的实体框架(6.1)构建了一个 POC 控制台应用程序。我采取了以下步骤
Ran Update-Database -verbose 确保运行添加索引的迁移。该指数使用了以下内容:
modelBuilder.Entity<LocationEntity>().Property(t => t.Coordinates).HasColumnAnnotation("Index", new IndexAnnotation(new IndexAttribute("ix_locationentity_coordinates")));
没有创建索引,但应用程序也没有崩溃。我可以尝试对此进行排列,但我的示例似乎遵循实体框架的约定:Official Fluent Documentation