Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple Index on same property? #2522

Closed
ziaulhasanhamim opened this issue Oct 11, 2022 · 4 comments
Closed

Multiple Index on same property? #2522

ziaulhasanhamim opened this issue Oct 11, 2022 · 4 comments

Comments

@ziaulhasanhamim
Copy link

ziaulhasanhamim commented Oct 11, 2022

I wanted to create two indexes on the same property. Like one GIN and the other one normal. But EF only generates sql for one index.
I found this issue on efcore repo. But didn't help. Because PostgreSQL supports multiple indexes on the same property. So why can't generate SQL for that?

builder.Entity<Customer>()
            .HasIndex(c => c.Name)
            .HasDatabaseName("IX_Customers_Name");

builder.Entity<Customer>()
            .HasIndex(c => c.Name)
            .HasMethod("GIN")
            .HasOperators("gin_trgm_ops")
            .HasDatabaseName("IX_Customers_Name_GIN");

Is it something I'm doing wrong here? I need both of these indexes. Because GIN helps a lot when running LIKE or ILIKE query(My applicated uses LIKE heavily) and also needs a normal index for the equal query.

@roji
Copy link
Member

roji commented Oct 11, 2022

EF can definitely generate this, but you need to define the name inside HasIndex, and not via HasDatabaseName:

builder.Entity<Customer>()
            .HasIndex(c => c.Name, "IX_Customers_Namer");

builder.Entity<Customer>()
            .HasIndex(c => c.Name, "IX_Customers_Name_GIN")
            .HasMethod("GIN")
            .HasOperators("gin_trgm_ops");

Let me know if that works for you. I've opened dotnet/EntityFramework.Docs#4081 to add this to the EF docs

@ziaulhasanhamim
Copy link
Author

It works like charm. Thanks

@roji roji closed this as not planned Won't fix, can't repro, duplicate, stale Oct 12, 2022
@prcdpr
Copy link

prcdpr commented Apr 18, 2023

I think this overload of HasIndex is unavailable when using OwnedNavigationBuilder. What are alternatives here?

@roji
Copy link
Member

roji commented Apr 18, 2023

@prcdpr can you please open an issue for this on the EF Core repo?

In the meantime, you can always create indexes via SQL in your migrations.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants