数据有一个字段,用的是 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.3064.CPU监控(线段树 历史最值)

    题目链接 \(Description\) 有一个长为n的序列Ai,要求支持查询[l,r]的最值.历史最值,区间加/重设 \(Solution\) 线段树,每个点再维护一个历史(从0到现在)最大值.历史 ...

  2. 2016年3月10日Android实习日记

    待解决问题: *1:内部ScrollView与外部手势事件滑动冲突问题. *2:Linearlayout+View+LinearLayout横向排列,这其中两个LinearLayout内部各有3个竖向 ...

  3. unity3d脚本语言中的引用类型

    在之前的文文里有说到,值类型和引用类型,那么这会就单独说下引用类型: Unity3D中的C#语言提供了专门的类型来为开发者提供使用C#开发游戏的便利条件: 在该引擎中,使用UnityEngine命名空 ...

  4. exce中42093和日期之间的关系

    在EXECEL中数字0 代表日期 1900-1-0 ,即这个日期为起始日期,算是第0天数字1 代表日期 1900-1-1 ,即第一天数字2 代表日期 1900-1-2 ,即第二天......数字415 ...

  5. Spark MLlib 之 aggregate和treeAggregate从原理到应用

    在阅读spark mllib源码的时候,发现一个出镜率很高的函数--aggregate和treeAggregate,比如matrix.columnSimilarities()中.为了好好理解这两个方法 ...

  6. getOutputStream() has already been called for this response解释以及解决方法

    异常:getOutputStream() has already been called for this response 的解决方法 今天在第一次接触使用“验证码”功能时,在执行时出现了异常信息: ...

  7. springmvc中的页面解析器ViewResolver不起作用,变量输出字符串的解决方案

    <web-app xmlns:web="http://xmlns.jcp.org/xml/ns/javaee"> <servlet> <servlet ...

  8. oracle-用户和表空间创建

    windows下 创建临时表空间 create temporary tablespace user_temp tempfile 'D:\oracle\oradata\Oracle11i\user_te ...

  9. 小度WiFi

    这个东西真不错,详情查看: http://wifi.baidu.com 是在京东上抢购的,但是那次抢购体验做得很次:首先,只能预约一种颜色;其次,第一天抢购了,第2天就不能抢购了;第三,等抢购完了,如 ...

  10. 启动apache (OS 10022)提供了一个无效的參数。解决方式

    今天 apache 突然启动不起来了,查看了一下错误日志发现了例如以下错误: [Tue Mar 17 11:27:32 2015] [crit] Parent: child process exite ...