EF Core设置Gin索引

EF Core中如何设置Gin索引?

https://stackoverflow.com/questions/54618858/can-jsonb-gin-indexes-be-specified-in-codefirst-entityframework-with-npgsql


Since .ForNpgsqlHasMethod(“gin”) is obsolete, with EF Core 3.1 you can use this:

1
2
3
4
metaBuilder
.HasIndex(x => x.DocumentNumber)
.HasMethod("gin")
.HasOperators("gin_trgm_ops");

and your migration will look like this:

1
2
3
4
5
6
migrationBuilder.CreateIndex(
name: "IX_DocumentContentMeta_DocumentNumber",
table: "DocumentContentMeta",
column: "DocumentNumber")
.Annotation("Npgsql:IndexMethod", "gin")
.Annotation("Npgsql:IndexOperators", new[] { "gin_trgm_ops" });

Well, actually u can do it like this

1
2
3
4
5
6
7
8
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Artist>()
.HasIndex(a => new { a.Album })
.ForNpgsqlHasMethod("gin");

base.OnModelCreating(modelBuilder);
}

and when u run ef add migration, the sql query generated will be like:

1
CREATE INDEX ix_artist_album ON artist USING gin ("album");

Documentation in Npgsql: http://www.npgsql.org/efcore/modeling/indexes.html


EF Core设置Gin索引
https://pygyme.com/PostgreSQL数据库/ef-core设置gin索引/
作者
PYGYME
发布于
2021年12月31日
许可协议