数据有一个字段,用的是 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
下面是一个老外的文章,http://forums.mysql.com/read.php?38,562960,562960 他用的是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。