dapper mysql tinyint,尝试设置从MySQL返回的布尔值时,Dapper抛出无效的强制转换异常...

I have this class

public class User

{

public int UserId { get; set; }

public string UserName { get; set; }

public bool IsValidated { get; set; }

}

And I'm populating it with this sql using dapper:

var users = connection.Query("SELECT userId, userName, TRUE `IsValidated` FROM user WHERE [...]").ToList();

When I run this I get this error:

Error parsing column 2 (IsValidated=1 - Int64)

I've stepped through the dapper code & the sqldatareader is saying that that column is int64, so it looks like the .NET Mysql Connector is thinking that 'TRUE' (which should be tinyint in MYSQL) is an int64.

I did find this bug report which said that for all versions of INT (INT, BIGINT, TINYINT, SMALLINT,MEDIUMINT) the .NET connector was returning int64. However this was a bug in MySQL 5.0 & was fixed, I'm using 5.5. I've got mysql.data version 6.4.3.0

I have "solved" this problem by selecting it all into a temporary table with the IsValidated column declared as BOOL, but this is a lousy solution.

解决方案

I'm not familiar with Drapper, but since MySQL will return any boolean as an int (normally tinyint), one option could be to change your class to the following:

public class User

{

public int UserId { get; set; }

public string UserName { get; set; }

private bool _isValidated = false;

public bool IsValidated

{

get{ return _isValidated;}

set{ _isValidated = Boolean.Parse(value); }

}

}

Alternatively try a cast in the sql

cast(TRUE `IsValidated` as bit)

I haven't tried this, but at least you have a suggestion. All the best.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值