Skip to content
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

Query: Invalid SQL generated for Guid literals on SQLite #5801

Closed
ajcvickers opened this issue Jun 17, 2016 · 5 comments
Closed

Query: Invalid SQL generated for Guid literals on SQLite #5801

ajcvickers opened this issue Jun 17, 2016 · 5 comments
Assignees
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-bug
Milestone

Comments

@ajcvickers
Copy link
Contributor

This works on SQL Server, but not on SQLite.

public class GuidKey
{
    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    public Guid Id { get; set; }

    public string Foo { get; set; }
}

public class FindContext : DbContext
{
    public DbSet<GuidKey> GuidKeys { get; set; }
}

[Fact]
public virtual void Find_guid_key()
{
    using (var context = new FindContext())
    {
        context.Add(
            new GuidKey 
            {
                Id = new Guid("{3FDFC4F5-AEAB-4D72-9C96-201E004349FA}"), 
                Foo = "Smokey" 
            });

        context.SaveChanges();
    }

    using (var context = new FindContext())
    {
        var entity = context.GuidKeys.FirstOrDefault(
            e => e.Id == new Guid("{3FDFC4F5-AEAB-4D72-9C96-201E004349FA}"));

        Assert.Null(entity); // Not found

        entity = context.GuidKeys.ToList().FirstOrDefault(
            e => e.Id == new Guid("{3FDFC4F5-AEAB-4D72-9C96-201E004349FA}"));

        Assert.NotNull(entity); // Found by LINQ to Objects
    }
}
PRAGMA foreign_keys=ON;

SELECT "e"."Id", "e"."Foo"
FROM "GuidKey" AS "e"
WHERE "e"."Id" = '3fdfc4f5-aeab-4d72-9c96-201e004349fa'
LIMIT 1
@ajcvickers
Copy link
Contributor Author

Note that this works, but only because it is client-evaluated:

var entity = context.GuidKeys.FirstOrDefault(
    e => e.Id.ToString() == new Guid("{3FDFC4F5-AEAB-4D72-9C96-201E004349FA}").ToString());

@natemcmaster
Copy link
Contributor

Related to aspnet/Microsoft.Data.Sqlite#191

@ajcvickers
Copy link
Contributor Author

@natemcmaster Related maybe, but in this case the database was created by EF and the query is fully in the form of Guid objects, not strings.

@bricelam
Copy link
Contributor

That literal is wrong. It should be x'f5c4df3fabae724d9c96201e004349fa'. Anywhere else we inline Guid values is also broken.

@bricelam bricelam changed the title Query: Sqlite: Simple FirstOrDefault does not compare Guids correctly SQLite Query: Guid literals are wrong Jun 23, 2016
@bricelam
Copy link
Contributor

There's another instance of of this on StackOverflow: How to linq filter on multiple GUID at once generating an appropriate sql where clause

bricelam added a commit that referenced this issue Jun 24, 2016
@rowanmiller rowanmiller changed the title SQLite Query: Guid literals are wrong SQLite Query: Invalid SQL generated for Guid literals Jun 29, 2016
@rowanmiller rowanmiller changed the title SQLite Query: Invalid SQL generated for Guid literals Query: Invalid SQL generated for Guid literals on SQLite Jul 21, 2016
@bricelam bricelam added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Aug 10, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-bug
Projects
None yet
Development

No branches or pull requests

5 participants