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.

Cannot filter on Guid column with SQLite provider

See original GitHub issue

Upon attempting to filter for a single record with a Guid PK, where such a record actually exists in a SQLite database, null is being returned. The Guid PK value is being stored as a Blob in the SQLite database, and it seems that the way EF Core is generating SQL from LINQ queries, does not allow for querying using Guid values - I must first convert Guid values to strings for filtering to successfully work.

Steps to reproduce

  • Using the PowerShell Add-Migration and Update-Database commands together with the SQLite DB provider, create a table (e.g. DbSet<Foo>) having a Guid PK property named Id.

  • Add a new Foo entry with some Guid PK value, then attempt to query for that entry using (where query is some object with Id as a Guid property):

    var foo = _context
        .Foos
        .SingleOrDefault(f => f.Id == query.Id);
    

    … and observe that foo is null.

  • Attempt to query for the entry using:

    var foo = _context
        .Foos
        .SingleOrDefault(f => f.Id.ToString() == query.Id.ToString());
    

    … and observe that the non-null entry is returned.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:2
  • Comments:16 (7 by maintainers)

github_iconTop GitHub Comments

4reactions
RouRcommented, May 26, 2019

This is not work.

var foo = _context
    .Foos
    .SingleOrDefault(f => f.SomeField == query.SomeField);

var connection = new SqliteConnection(“DataSource=:memory:”);

Microsoft.EntityFrameworkCore.Sqlite 2.2.4

There is one very important difference between .SingleOrDefault(f => f.SomeField == query.SomeField); and .SingleOrDefault(f => f.SomeField.ToString() == query.SomeField.ToString()); - where it will be evaluated . Microsoft.EntityFrameworkCore.Query.QueryClientEvaluationWarning: The LINQ expression ‘where ([x].SomeField .ToString() == __ToString_1)’ could not be translated and will be evaluated locally.’

This LINQ must work - .SingleOrDefault(f => f.SomeField == query.SomeField); But it does not.

0reactions
RouRcommented, May 27, 2019

My mistake. I did some investigation. It seem that Sqlite-in-memory does not support raw sql queries. So, some logic changes in DB was skip, and result query is not valid. Not valid query returns null.

I guess it need a warning in log for context.Database.GetDbConnection().CreateCommand() and context.SomeEntities.FromSql

Some tests for play with it http://github.com/RouR/Sqlite-InMemoryTest

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using guid in sqlite select where guid is stored in the sqlite ...
The GUID is probably being stored as a binary blob; try: SELECT * FROM Employee WHERE Employee.Id = X'a8828ddfef224d36935a1c66ae86ebb3';.
Read more >
Failed Field filter on GUID column is SQL Server
Hi, I was trying to create a field filter to filter table's ID column, but in preview I see that filtered string casted...
Read more >
sqlite - Update function to enter random/unique GUID per row
In other words, Update my table and set the GUID column with the value of 2. The WHERE clause filters which rows it...
Read more >
SQLite FTS5 Extension
Nested column filter operations may only further restrict the subset of columns matched, they can not be used to re-enable filtered columns. For...
Read more >
Value Conversions - EF Core
Configuring value converters in an Entity Framework Core model.
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