Search code examples
entity-framework.net-coreentity-framework-corenpgsqlef-core-2.1

How to create unique index case insensitive at entity framework core 2.1


I use npqsql 4+ and efcore 2.1+, how I can create unique invariant case insensetive constraint on field?

The following construction doesn't work:

modelBuilder.Entity<Company>().HasAlternateKey(city => city.Name.ToUpperInvariant());

Solution

  • Unfortunately, there doesn't appear to be a way to create an index with a specific collation using the Npgsql driver. The only PostgreSQL specific configuration that can be done on an index is to set the method using the ForNpgsqlHasMethod extension. You have a couple of options:

    1. Create/modify the index with some SQL, perhaps as part of a migration
    2. Go an request the feature on the official Github project, or even write your own and submit a pull request!