为什么在执行选择时dapper却为Guid返回全零,但表中的guid值设置正确? [英] Why is dapper returning all zero's for Guid when doing a select but the guid value in table is set correctly?

查看:361
本文介绍了为什么在执行选择时dapper却为Guid返回全零,但表中的guid值设置正确?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用精简工具从表格中查询数据,然后将其转换为对象。当将其强制转换为对象时,guid属性设置为全零,但所有其他道具均设置为正确。

  public class UserStuff 
{
public int Id {get;组; }
公共Guid UId {get;组; }
}


公共异步任务< UserStuff> GetUserStuff(Guid uId){
using(IDbConnection conn = Connection){
string sQuery = SELECT TOP 100 id,u_id +
FROM TestTable WHERE u_id = @u_id;
conn.Open();
var result = await conn.QueryAsync< UserStuff>(sQuery,new {u_id = uId});
return result.FirstOrDefault();
}
}

示例SQL数据:



id | u_id



5 | C9DB345B-D460-4D71-87E0-D9A3B5CE1177



返回:
5为id,全零为guid

解决方案

此处的核心问题是您的列名和属性名不同。因此,即使数据库通过SQL查询返回该值,也不会将其映射到您的属性。由于属性的数据类型为 GUID ,因此它保留其默认值-全零。



Dapper映射适用于约定;讨论的话题比较广泛。对于您的特定问题,您的列名称和属性名称应匹配。如果映射不同,还有其他方法可以使映射正确发生。



我将在此处提出简单的解决方案:


  1. 您可以指示Dapper在映射时忽略列名中的下划线。

     使用Dapper; 
    Dapper.DefaultTypeMap.MatchNamesWithUnderscores = true;

    在项目启动时的某个地方设置此属性。


  2. 在SQL查询中使用列别名。将您的SQL查询更改为以下内容:

     选择前100个id,u_id为UId...。

    这样,无需更改列名和属性名,就可以正确填充属性,因为映射是


  3. 更改列名称以与属性名称匹配,反之亦然。我不认为这是切实可行的。 / p>

    I'm using dapper to query data from table and then cast it to an object. When it is cast to the object the guid property is set to all zero's, but all the other props are set correct.

    public class UserStuff
    {
        public int Id { get; set; }
        public Guid UId { get; set; }
    }
    
    
    public async Task<UserStuff> GetUserStuff(Guid uId){
      using(IDbConnection conn = Connection){
        string sQuery = "SELECT TOP 100 id, u_id " +
                        "FROM TestTable WHERE u_id = @u_id ";
        conn.Open();
        var result = await conn.QueryAsync<UserStuff>(sQuery, new { u_id = uId });
        return result.FirstOrDefault();
      }
    }
    

    Example SQL data:

    id | u_id

    5 | C9DB345B-D460-4D71-87E0-D9A3B5CE1177

    It's returning : 5 for the id, and all zero's for the guid

    解决方案

    Core problem here is that your column name and property name are different. Hence, even though the value is returned by database as a result of SQL query, it is not mapped to your property. As the datatype of your property is GUID, it holds its default value - all zeros.

    Dapper mapping works on conventions; bit broad topic to discuss. For your specific problem, your column name and property name should match. There are other ways as well to make the mapping happen correctly if those are different.

    I will propose simple solutions here:

    1. You can instruct Dapper to ignore underscores from column name while mapping.

      using Dapper;
      Dapper.DefaultTypeMap.MatchNamesWithUnderscores = true;
      

      Set this property at the startup of the project somewhere.

    2. Use column aliases in SQL query. Change your SQL query to something like below:

      SELECT TOP 100 id, u_id as UId....
      

      This way, without changing the column name and property name, you will be able to fill up the property correctly as the mapping is corrected now.

    3. Either change the column name to match with property name or vice versa. I do not think this is practical.

    Apart for above, there is also a custom mapping available with dapper.

    这篇关于为什么在执行选择时dapper却为Guid返回全零,但表中的guid值设置正确?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆