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.

1
  • What EF approach do you use? Commented May 1, 2016 at 14:30

2 Answers 2

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.*)

Sign up to request clarification or add additional context in comments.

6 Comments

I get, "Specified method is not supported".
@IanWarburton I need more details about this exception. Can you paste full stacktrace? What version of EntityFramework, EntityFramework6.Npgsql and Npgsql do you have?
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'
Looking at the source, that method doesn't even seem to handle custom functions.
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.*)
|
2

Net6 this work for me

.Where(x => people.ILike(EF.Functions.Unaccent(x.Name)....

1 Comment

Remember to install the extension on the database with "CREATE EXTENSION unaccent;" command, or directly on the OnModelCreating(ModelBuilder builder) { builder.HasPostgresExtension("unaccent"); }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.