数据有一个字段,用的是 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。

Entity Framework 5中遇到的 mysql tinyint(1) 转换为 bool 的问题 (我用的是VS2013中的EF5版本)的更多相关文章

  1. 创建ASP.NET Core MVC应用程序(3)-基于Entity Framework Core(Code First)创建MySQL数据库表

    创建ASP.NET Core MVC应用程序(3)-基于Entity Framework Core(Code First)创建MySQL数据库表 创建数据模型类(POCO类) 在Models文件夹下添 ...

  2. MVC4,MVC3,VS2012+ entity framework Migration from Sqlserver to Mysql

    在开发的初期个人认为因VS与Sqlserver的配合很默契,即可以方便的实现Code First,又可以使用SqlServer Manager很漂亮的进行建模与变更,也许是个人的使用习惯MS的界面做的 ...

  3. 《Entity Framework 6 Recipes》中文翻译系列 (40) ------ 第七章 使用对象服务之从跟踪器中获取实体与从命令行生成模型(想解决EF第一次查询慢的,请阅读)

    翻译的初衷以及为什么选择<Entity Framework 6 Recipes>来学习,请看本系列开篇 7-5  从跟踪器中获取实体 问题 你想创建一个扩展方法,从跟踪器中获取实体,用于数 ...

  4. 安装使用Entity Framework Power Tool Bate4 (Code First)从已建好的数据自动生成项目中的对应Model(新手贴,望各位大侠给予指点)

    从开始学习使用MVC以后,同时也开始接触EF,很多原理都不是太懂,只知道安装了EF以后,点击哪里可以生成数据库对应的Model,不用再自己手写Model.这里记录的就是如何从已建立好的数据库生成项目代 ...

  5. Entity Framework 6 Code First +MVC5+MySql/Oracle使用过程中的几个问题

    1. namespace Snapsia.Web.Models { using System; using System.Data.Entity; using System.ComponentMode ...

  6. Entity Framework插入中文数据到MySQL乱码问题

    1.MYSQL: 保证所有的的列都是UTF8格式. 2.在连接MySQL的连接字符串中,加入配置文件信息:Character Set=utf8 

  7. 了解Entity Framework中事务处理

    Entity Framework 6以前,框架本身并没有提供显式的事务处理方案,在EF6中提供了事务处理的API. 所有版本的EF,只要你调用SaveChanges方法进行插入.修改或删除,EF框架会 ...

  8. How to: Use Both Entity Framework and XPO in a Single Application 如何:在单个应用程序中同时使用实体框架和 XPO

    This topic demonstrates how to create a simple XAF application that uses both the Entity Framework ( ...

  9. ASP.NET没有魔法——ASP.NET MVC 与数据库之Entity Framework Migrations

    在开发数据库应用程序的时候,经常会遇到某些表需要添加字段或者修改类型.新增表等需求,而对于EF Code First来说关注的只有实体类,当需求变更时只需要添加新的实体类或者在实体类中添加.删除.修改 ...

随机推荐

  1. BZOJ.5404.party(树链剖分 bitset Hall定理)

    题目链接 只有指向父节点的单向道路,所以c个人肯定在LCA处汇合.那么就成了有c条到LCA的路径,求最大的x,满足能从c条路径中各选出x个数,且它们不同. 先要维护一条路径的数的种类数,可以树剖+每条 ...

  2. centos6.9 忘记密码解决方法

    若果忘记了 root 的密码,解决方法如下: 我采用的 linux 版本是 centos-6.9 , 经过亲身实践证明,该方法是 ok 的 在开机启动的时候按键盘上的“E”键会进入如下界面. 选择相应 ...

  3. C#基础_循环

    1,三元运算符 表达式1?表达式2:表达式3 栗子1:bool result=5>3?true:false; 栗子2: int num=5>3?1:0; 注意事项以及相应规则: 2, 3, ...

  4. 喵哈哈村的魔法考试 Round #15 (Div.2) 题解

    哗啦啦村的奇迹果实(一) 题解:显然答案就是最大值减去最小值. #include<bits/stdc++.h> using namespace std; const int maxn = ...

  5. Web App 和 Native App,哪个是趋势?

    一.Web App vs. Native App 比起手机App,网站有一些明显的优点. 跨平台:所有系统都能运行 免安装:打开浏览器,就能使用 快速部署:升级只需在服务器更新代码 超链接:可以与其他 ...

  6. Eclipse 4.5.0 离线安装 Veloeclipse 插件

    下载 Veloeclipse 在 Eclipse eclipse-jee-mars-R-win32-x86_64 版本 4.5.0,Build id 为 20150621-1200,离线安装 Velo ...

  7. zabbix用户管理

    zabbix用户管理,主要包括用户增删改查.用户报警媒介管理.用户权限管理. 安装完zabbix后,已经自带了两个用户Admin和Guests 超级管理员默认账号:Admin,密码:zabbix,这是 ...

  8. 浅谈常用的几种web攻击方式

    一.Dos攻击(Denial of Service attack) 是一种针对服务器的能够让服务器呈现静止状态的攻击方式.有时候也加服务停止攻击或拒绝服务攻击.其原理就是发送大量的合法请求到服务器,服 ...

  9. Database数据库切片模式

    数据库切片模式关注的实现水平伸缩.切分是从单个数据库到平分数据访问两个或更多数据库切片.每个切片有和原始数据库相同的Schema.大多数据分布在每个切片每一行.从切片合并起来的数据和原始数据库一样.切 ...

  10. Lua:Nginx Lua环境配置,第一个Nginx Lua代码

    一.编译安装LuaJIT Lua:编译安装LuaJIT,第一个Lua程序 http://blog.csdn.net/guowenyan001/article/details/48250427 二.下载 ...