Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can JSONB GIN indexes be specified in CodeFirst EntityFramework with NPGSQL?

I've got some JSONB columns on my EF code first objects in an EFCore project. I would like to set up some GIN indexes to properties inside those strongly typed objects being stored as JSONB property/column.
Is this possible? (currently using the postgres:latest image)

Thanks.

like image 301
Bob Avatar asked Dec 23 '22 01:12

Bob


2 Answers

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

metaBuilder
    .HasIndex(x => x.DocumentNumber)
    .HasMethod("gin")
    .HasOperators("gin_trgm_ops");

and your migration will look like this:

migrationBuilder.CreateIndex(
    name: "IX_DocumentContentMeta_DocumentNumber",
    table: "DocumentContentMeta",
    column: "DocumentNumber")
    .Annotation("Npgsql:IndexMethod", "gin")
    .Annotation("Npgsql:IndexOperators", new[] { "gin_trgm_ops" });
like image 58
Sam Sch Avatar answered Dec 28 '22 09:12

Sam Sch


Well, actually u can do it like this

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:

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

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

like image 40
hellokitty5685 Avatar answered Dec 28 '22 09:12

hellokitty5685