question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

SQLite: Lower-case Guid strings don't work in queries

See original GitHub issue

I use Microsoft.EntityFrameworkCore.Sqlite 3.1.1 in a testing project (with SqlServer used elsewhere). I expect it to be a (mostly) drop-in alternative. However, this doesn’t work as expected with SQLite (but works fine with SqlServer and InMemory):

public IQueryable<Item> Query(Guid? tenantId, string? tag = null)
		{
			var entities = this.dbContext.Items
					.Include(a => a.Tags)
					.Where(a => tag == null || a.Tags.Any(at => at.TenantId == tenantId && at.Tag == tag))
               ...

entities is empty, while it’s expected to contain some items. Upon checking a whole bunch of Guid-related issues here, the only workaround I found requires changing OnModelCreating (adding .HasConversion<string>() for the property helps), which is not acceptable since the same code works with other providers. Therefore, I conclude that it’s a bug in the provider.

Tag.TenantId is Guid?. Database was created using context.Database.EnsureCreated();. GUIDs are formatted as 00000000-0000-0000-0000-000000000000 (as per https://docs.microsoft.com/en-us/dotnet/standard/data/sqlite/types).

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:1
  • Comments:24 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
bricelamcommented, Nov 9, 2020

To mitigate future breaking changes in this area, I’d recommend converting all the database values to lowercase and using a value converter.

UPDATE MyTable
SET GuidColumn = lower(GuidColumn);
modelBuilder
    .Entity<MyEntity>()
    .Property(e => e.GuidProperty)
    .HasConversion(
        g => g.ToString(),
        s => new Guid(s));
0reactions
delasourcecommented, Oct 19, 2022

Comparing two Guid types is handled fine. I personally like to have Guid.TryParse on the outside of that line and have invalid formats checked beforehand. (Guid.Parse will throw)

Yes, AsEnumerable (even though it supposedly “streams” the data) is an impact on performance on large result sets, which you obviously have, since you need to query all elements of your DbSet. (according to documentation)

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to set Sqlite3 to be case insensitive when string ...
(A bug: SQLite only understands upper/lower case for ASCII characters. The LIKE operator is case sensitive for unicode characters that are ...
Read more >
3 Ways to Find Rows that Contain Lowercase Letters in ...
Option 1: Compare to the UPPER() String. We can use the UPPER() function to compare the original value to its uppercase equivalent: SELECT ......
Read more >
SQLite lower() function
SQLite lower() converts all the characters in a string to lowercase characters. The lower(str) function returns a copy of string str with all ......
Read more >
Built-In Scalar SQL Functions
The lower(X) function returns a copy of string X with all ASCII characters converted to lower case. The default built-in lower() function works...
Read more >
How to Change Text to Lowercase in SQL
Whenever you want some text data from your SQL database to be displayed in lowercase, use the LOWER() function. This function takes as...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found