2

I have this method:

    public static IEnumerable<T> GetList(string where, Dictionary<string, object> parameters)
    {
        IEnumerable<T> entities;
        using (var connection = OpenConnection())
        {
            entities = connection.GetList<T>(where, new DynamicParameters(parameters));
        }
        return entities;
    }

And I call it like such:

string publicID = "463EC1EE-8AAB-4ABA-9B39-132BC8D3236E"
Dictionary<string, object> parameters = new Dictionary<string, object>();
parameters.Add("@APIPublicID", publicID);
var apiUsers = Repository<APIUsers>.GetList("WHERE APIPublicID = @APIPublicID", parameters).ToList();

The GetList() method calls the SIMPLECrud .dll that is a wrapper over Dapper.

That works beautifully. But here's the really weird thing. If I add some extra letters or numbers on the end of the guid publicID, it STILL works:

Instead of:

string publicID = "463EC1EE-8AAB-4ABA-9B39-132BC8D3236E"

I do...

string publicID = "463EC1EE-8AAB-4ABA-9B39-132BC8D3236EABCDEFG"
.... // rest of the method as before

I get the exact same results. If I make the guid shorter, or if I change characters inside of it, then it behaves as expected.

What am I doing wrong here?

Casey Crookston
  • 13,016
  • 24
  • 107
  • 193
  • It may be worth trying to create a small example to isolate whether this is a fault in SimpleCrud's handling of Guids or maybe it's in the main Dapper codebase. Then I would go digging through the source code or assemblies to find it. The last time I did this was when Dapper was asked to fetch a string value from a database, and one particular string in that column was a string containing 'N' then 'U' then 'L' then 'L', and Dapper returned it as a null rather than the string. – Richardissimo Mar 12 '18 at 21:24

1 Answers1

2

I think it's not related to dapper or SIMPLECrud but how SQL Server converts strings to uniqueidentifier. I assume that you use SQL Server but if not - probably your database behaves in similar way.

When casting string to uniqueidentifier, SQL server will just ignore exsessive characters:

select cast('463EC1EE-8AAB-4ABA-9B39-132BC8D3236EABCDEFG' as uniqueidentifier)
-- no errors, returns 463EC1EE-8AAB-4ABA-9B39-132BC8D3236E

That means if APIPublicID column in your example is of type uniqueidentifier, the following queries will behave the same:

select * from MyTable WHERE APIPublicID = '463EC1EE-8AAB-4ABA-9B39-132BC8D3236E'
select * from MyTable WHERE APIPublicID = '463EC1EE-8AAB-4ABA-9B39-132BC8D3236EABCDEFG'

Because to compare, they have to be of the same type, so your string is converted to uniqueidentifier, ignoring excessive part (ABCDEFG).

Evk
  • 98,527
  • 8
  • 141
  • 191