Cannot filter on Guid column with SQLite provider
See original GitHub issueUpon 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 namedId
. -
Add a new
Foo
entry with some Guid PK value, then attempt to query for that entry using (wherequery
is some object withId
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:
- Created 6 years ago
- Reactions:2
- Comments:16 (7 by maintainers)
This is not work.
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.
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