我在EFCore项目中的EF代码优先对象上有一些JSONB列。我想将存储为JSONB属性/列的强类型对象中的某些属性设置为GIN索引。
这可能吗?(目前正在使用postgres:latest镜像)
谢谢。
我在EFCore项目中的EF代码优先对象上有一些JSONB列。我想将存储为JSONB属性/列的强类型对象中的某些属性设置为GIN索引。
这可能吗?(目前正在使用postgres:latest镜像)
谢谢。
由于 .ForNpgsqlHasMethod("gin")
已过时,因此在 EF Core 3.1 中你可以使用以下方式:
metaBuilder
.HasIndex(x => x.DocumentNumber)
.HasMethod("gin")
.HasOperators("gin_trgm_ops");
你的迁移将如下所示:
migrationBuilder.CreateIndex(
name: "IX_DocumentContentMeta_DocumentNumber",
table: "DocumentContentMeta",
column: "DocumentNumber")
.Annotation("Npgsql:IndexMethod", "gin")
.Annotation("Npgsql:IndexOperators", new[] { "gin_trgm_ops" });
嗯,实际上你可以这样做
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Artist>()
.HasIndex(a => new { a.Album })
.ForNpgsqlHasMethod("gin");
base.OnModelCreating(modelBuilder);
}
CREATE INDEX ix_artist_album ON artist USING gin ("album");
Npgsql文档中的说明:http://www.npgsql.org/efcore/modeling/indexes.htmlmodelBuilder.Entity<MyAwesomeModel>()
.HasIndex(f => f.stringToTriGram)
.HasMethod("gin")
.HasOperators("gin_trgm_ops");
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.HasPostgresExtension("pg_trgm");
}
通过Npgsql EF6提供程序没有特定的方法设置GIN索引。但是,您可以在迁移中使用原始SQL手动设置它。
但是,正如@hellokitty5685在另一个答案中所写的那样,这在EF Core中是可能的。
CREATE EXTENSION pg_trgm;
。 - Sam Sch