【发布时间】:2014-09-05 04:14:24
【问题描述】:
嗯,这个问题已经很清楚了。是否可以使用 Entity Framework 6.1 fluent API 创建空间索引?
【问题讨论】:
标签: entity-framework spatial spatial-index entity-framework-6.1 ef-fluent-api
嗯,这个问题已经很清楚了。是否可以使用 Entity Framework 6.1 fluent API 创建空间索引?
【问题讨论】:
标签: entity-framework spatial spatial-index entity-framework-6.1 ef-fluent-api
我知道的唯一方法是通过“自定义”迁移。在 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]");
}
}
不是一个理想的方法,但也不算太糟糕,因为它确实遵循 EF 的“正常”迁移模式。
【讨论】:
[Index] 作为Data Annotations 为DbGeography 更新数据库时会出现以下错误:Column 'Area' in table 'dbo.YourTable' is of a type that is invalid for use as a key column in an index or statistics.
简短的回答-不,不是。我在blogs 中看到了这个切线引用,但没有找到具体的实现示例。这似乎与空间索引是过滤索引的事实有关,实体框架不支持。
作为对我的回答的支持,我使用最新版本的 Entity Framework (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
【讨论】: