Mysql tinyint长度为1时在java中被转化成boolean型

数据有一个字段,用的是 tinyint 长度是1  默认值为0 ,

当用vs2013中的 EF5来生成 实体模型之后,看到这个列被标识为 bool 类型

 

Mysql官方参考文档关于布尔类型的说明:

BOOL, BOOLEAN

These types are synonyms(同义词) for TINYINT(1). A value of zero is considered(认为是) false. Nonzero(不为0) values are considered true

 

下面是一个老外的文章,MySQL :: MySQL Connector/Net for EF binds TINYINT(1) incorrectly  他用的是EF4.3版本,我用的是EF5版本

 

I believe I've discovered a bug in the way the MySQL Connector for .NET maps fields of type TINYINT(1) within Entity Framework.
The database I'm working with has a number of fields used to store "enumeration values" - basically an integer that represents a specific .NET enumeration value. ie/ Open = 1, Closed = 2
Since the enumerations contain a small number of possible values (2-5), the majority of these fields are declared as the MySQL datatype TINYINT(1). In other words, we want an integer with a minimal amount of storage space and a maximum of "one character".
When we use Entity Framework 4.3 to map these TINYINT(1) fields to an "int" data type, the integer value *always* comes back as "1", regardless of the underlying storage value. The integer values 2, 3, 4, etc all get converted to 1.
If I convert the entity property's type to "string", it receives a value of "True".
It appears as though the MySQL Connector for .NET is hardcoded to treat TINYINT(1) as a boolean, regardless of the data type it's eventually bound to. TINYINT(1) should only be converted to a boolean when it's bound to a "bool" property, and nothing else. It appears as though it's trying to simulate the behavior of the "BIT" field in SQL Server, which is a completely different thing (it's not an integer, while TINYINT is).
Is this a known issue? Should I file a bug report?

翻译过来如下

他说,在EF4.3中,他用一个字段 Tinyint(1) 想去存一个 int类型的值,不管是存 1还是2,3,4 他都是变成了1

如果他在Ef的实体中把类型改为string,到了数据库却变成了 “True”

这个问题好像是发生在,通过 MySql Connector For .Net这个组件来链接Mysql的时候发生的 .

解决方法 .

Tinyint(1)  就只用来保存 bool 值  只有0和1  不要保存其他的值

如果要保存多的值,就用 Tinyint(4) 这样的,那么到了Ef中这个 类型会变成  sbyte ,这个就是一个整形 .

备注:sbyte:存储8位带符号整数。sbyte中的s代表带符号(signed),意味着变量可为正值或负值。sbyte变量的最小可能值为-128,最大可能值为127

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值