3

Is possible to use Npgsql and Entity Framework 6 for query PostgreSQL ignoring accents? In play SQL it's possible to use the unaccent extension and could be indexed with a index also based in unaccent:

select * from users where unaccent(name) = unaccent('João')

In previous projects using MySql I could solve this problem just using a collation accent insensitive like utf8_swedish_ci but PostgreSQL lacks this kind of solution as far as I know.

Max Bündchen
  • 1,283
  • 17
  • 38

2 Answers2

7

If you use the Codefirst approach, you should try to use EntityFramework.CodeFirstStoreFunctions.

  1. First add EntityFramework.CodeFirstStoreFunctions to your project
  2. Add a custom convention with unaccent to DbModelBuilder
  3. Use it in a query.

Example of database context:

public class DatabaseContext : DbContext
{
    public DatabaseContext () : base("Context")
    {
        Database.SetInitializer<DatabaseContext>(null);
    }

    public DbSet<User> Users { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.HasDefaultSchema("public");
        modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();

        /** Adding unaccent **/           
        modelBuilder.Conventions.Add(new CodeFirstStoreFunctions.FunctionsConvention<DatabaseContext>("public"));
    }

    [DbFunction("CodeFirstDatabaseSchema", "unaccent")]
    public string Unaccent(string value)
    {
        // no need to provide an implementation
        throw new NotSupportedException();
    }
}

Example of usage:

var users = ctx.Users
               .Where(elem => ctx.Unaccent(elem.FirstName) == ctx.Unaccent("João"))
               .ToList();

Important notice:
This solution works with EntityFramework6.Npgsql (which uses Npgsql 3.*).
It doesn't work with Npgsql.EntityFramework (which uses Npgsql 2.*)

PiKos
  • 1,344
  • 1
  • 16
  • 15
  • I get, "Specified method is not supported". – Ian Warburton Jun 06 '16 at 22:08
  • 1
    @IanWarburton I need more details about this exception. Can you paste full stacktrace? What version of EntityFramework, EntityFramework6.Npgsql and Npgsql do you have? – PiKos Jun 06 '16 at 22:36
  • EF 6, Npgsql 2.2.5. Failing at Npgsql.SqlGenerators.SqlBaseGenerator.VisitFunction(EdmFunction function, IList`1 args, TypeUsage resultType) with 'System.Data.Entity.Core.EntityCommandCompilationException' – Ian Warburton Jun 07 '16 at 00:10
  • Looking at the source, that method doesn't even seem to handle custom functions. – Ian Warburton Jun 07 '16 at 00:25
  • 1
    I believe it was added in package EntityFramework6.Npgsql so you have to remove Npgsql.EntityFramework and switch to EntityFramework6.Npgsql (which uses Npgsql in version 3.1.*) – PiKos Jun 07 '16 at 00:53
  • Yes, it works now, although I had to install the latest Npgsql by itself first. I'm not sure why. It couldn't find the requested version before. Thanks. – Ian Warburton Jun 07 '16 at 16:32
2

Net6 this work for me

.Where(x => people.ILike(EF.Functions.Unaccent(x.Name)....
Juver Paredes
  • 124
  • 1
  • 2
  • Remember to install the extension on the database with "CREATE EXTENSION unaccent;" command, or directly on the OnModelCreating(ModelBuilder builder) { builder.HasPostgresExtension("unaccent"); } – Bernardo Soccal May 26 '23 at 14:52