PetaPoco ExecuteScalar with nullable Guid

Viewed 1441

I'm scratching my head trying to work out why petapoco's "Exists()" returns a false where it should return true. I use something like this to test:

A simple petapoco model:

[TableName("WorkLog")]
[PrimaryKey("Id")]
public class WorkLog : DefaultConnectionDB.Record<WorkLog>
{
     public int Id { get; set; }
     public Guid? Customer { get; set; }
     public Guid? Project { get; set; }
}

A database table with the same structure as above, filled with Id's and Customers (only nulls for Project).

Create a test object:

var x = new WorkLog {Id = 0, Customer = 'xxx-xxx-xxx...', Project = null}

Query if a the Worklog exists in DB, based on "Project":

var exist = db.Exists<WorkLog>("Project=@p", new { p = x.Project });

The result is "false" in this scenario. If I fill model, db and query with a Guid for Project it returns "true" like it should though.

So I guess this has something to do with null and DbNull comparison, but I can't seem to find the right place to dig. I've tried with a helper method for converting "null" to "DbNull" before query, but it still returns false.

Temporary solution:

Currently I've solved this by doing...

var exist = db.Exists<WorkLog>(x.Project == null ? "Project IS NULL" : "Project=@p", new { p = x.Project });

This now works as expected, but I'd rather the method could be called normally and still return the expected result.

1 Answers
Related