2

我有这个方法:

    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;
    }

我这样称呼它:

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();

GetList()方法调用 SIMPLECrud .dll,它是 Dapper 的包装器。

这很好用。但这是真正奇怪的事情。如果我在 guid 的末尾添加一些额外的字母或数字publicID,它仍然有效:

代替:

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

我愿意...

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

我得到完全相同的结果。如果我使 guid 更短,或者如果我更改其中的字符,那么它会按预期运行。

我在这里做错了什么?

4

1 回答 1

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).

于 2018-03-13T08:39:19.503 回答