数据有一个字段,用的是 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. 大文件拆分方案的java实践(附源码)

    引子 大文件拆分问题涉及到io处理.并发编程.生产者/消费者模式的理解,是一个很好的综合应用场景,为此,花点时间做一些实践,对相关的知识做一次梳理和集成,总结一些共性的处理方案和思路,以供后续工作中借 ...

  2. SIGALRM信号和SIGCHID信号

    在进行堵塞式系统调用时.为避免进程陷入无限期的等待,能够为这些堵塞式系统调用设置定时器.Linux提供了alarm系统调用和SIGALRM信号实现这个功能. 要使用定时器.首先要安装SIGALRM信号 ...

  3. Linux:修改和删除已有变量

    变量修改 变量的修改有以下几种方式: 变量设置方式 说明 ${变量名#匹配字串} 从头向后开始匹配,删除符合匹配字串的最短数据 ${变量名##匹配字串} 从头向后开始匹配,删除符合匹配字串的最长数据 ...

  4. svn与git操作对比 (未来有空做一个 svn与git实战对比 )

    svn是集中式的,git是分布式的,但是我们日常使用的都是按照集中式唯一服务器仓库的方式来去做的,最终我们的代码都要提交到一个唯一仓库中. 他们最大的区别是本地工作拷贝的工作方式不同, 一.svn本地 ...

  5. yum离线安装rpm包

    CentOS利用yum下载好rpm包,并离线安装   1.联网安装好rpm包,并将下载好的包备好 #yum install --downloadonly --downloaddir=/home/sam ...

  6. boost python3依赖安装

    之前写过python dlib依赖的安装,当时是使用了系统默认的python2.7安装的boost.Python,如果自己编译好python3,想安装支持python3的boost.Python也是可 ...

  7. zabbix用户管理

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

  8. RMAN-05541: no archived logs found in target database

    执行 duplicate target databaseto orcl from active database nofilenamecheck报错如下: RMAN> duplicate tar ...

  9. 用ctrl+鼠标滚动调节字体大小

    如此设置之后,按住ctrl+鼠标滚动,可以放大和变小代码的字号.

  10. supervisor //todo

    #安装easy-installyum install python-setuptools #安装 supervisoreasy_install supervisor #创建主配置文件echo_supe ...