-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
How to use case-insensitive query with Sqlite provider ? #11414
Comments
Some users have been successful by configuring the property like this: (although it's certainly not how the API was intended to be used) modelBuilder.Entity<X>().Property(x => x.Email).HasColumnType("TEXT COLLATE NOCASE"); |
Currently, the only way to do it per-query (not configuring the column) is by using raw SQL: db.X.FromSql($"SELECT * FROM X WHERE Email = {myvariable} COLLATE NOCASE").Where(...); |
Also note, SQLite only performs case-insensitive comparisons of ASCII characters. Override the default to include all Unicode characters: var connection = (SqliteConnection)db.Database.GetDbConnection();
connection.CreateCollation("NOCASE", (x, y) => string.Compare(x, y, ignoreCase: true)); |
@bricelam thanks a lot, |
The last code listing uses SQLite's ADO.NET APIs (the layer beneath EF Core) to replace SQLite's built-in Yes, my first two comments also apply to EF Core 1.x. |
@bricelam Thanks again.
Is that because Sqlite's ADO.Net provider is also the database engine so it can do that ? otherwise AFAK doing comparison in "client" side is pointless . |
SQLite is an in-process database. The snippet above works by passing a function pointer to SQLite that it can use to invoke the lambda. So technically, it is happening "on the server". |
This is different from EF's client-evaluated queries where extra data may be retrieved from the server so additional processing can be done on the client before returning the final result set. |
I walk through the differences between client-eval and passing pointers to SQLite in my SQLite & EF Core: UDF all the things! post. |
@bricelam Can this be closed? |
Yep. @John0King feel free to continue the conversation here even though the issue is closed. |
Quick question, when should I execute the code:
And, is it enough or I also need to use the .HasColumnType("TEXT COLLATE NOCASE"); trick? If I do it in the OnModelCreating I get a CreateCollation can only be called when the connection is open. execption |
I have had luck with using |
For anyone seeing this, since this issue was opened, EF Core added support for collations - see this page. |
Great! I've learned something new. |
I set a collation for a column to "NOCASE" and confirmed in SQLiteStudio that it was set properly. |
@winstond please open a new issue with a minimal, runnable repro so we can see what exactly you're doing. |
I'm not sure if I remember wrongly,
I see the
COLLATE NOCASE
append in the sql query very long time ago (may be notCOLLATE NOCASE
,but something else , all I remember is my query is case insensitive),but recently I use Sqlite again , and it is case sensitive , and I can not compare an "Email" filed with
x.Email == myvariable
The text was updated successfully, but these errors were encountered: