efcore: Cannot filter on Guid column with SQLite provider

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.

About this issue

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

Most upvoted comments

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.