Entity Framework 5中遇到的 mysql tinyint(1) 转换为 bool 的问题 (我用的是VS2013中的EF5版本)
数据有一个字段,用的是 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版本)的更多相关文章
- 创建ASP.NET Core MVC应用程序(3)-基于Entity Framework Core(Code First)创建MySQL数据库表
创建ASP.NET Core MVC应用程序(3)-基于Entity Framework Core(Code First)创建MySQL数据库表 创建数据模型类(POCO类) 在Models文件夹下添 ...
- MVC4,MVC3,VS2012+ entity framework Migration from Sqlserver to Mysql
在开发的初期个人认为因VS与Sqlserver的配合很默契,即可以方便的实现Code First,又可以使用SqlServer Manager很漂亮的进行建模与变更,也许是个人的使用习惯MS的界面做的 ...
- 《Entity Framework 6 Recipes》中文翻译系列 (40) ------ 第七章 使用对象服务之从跟踪器中获取实体与从命令行生成模型(想解决EF第一次查询慢的,请阅读)
翻译的初衷以及为什么选择<Entity Framework 6 Recipes>来学习,请看本系列开篇 7-5 从跟踪器中获取实体 问题 你想创建一个扩展方法,从跟踪器中获取实体,用于数 ...
- 安装使用Entity Framework Power Tool Bate4 (Code First)从已建好的数据自动生成项目中的对应Model(新手贴,望各位大侠给予指点)
从开始学习使用MVC以后,同时也开始接触EF,很多原理都不是太懂,只知道安装了EF以后,点击哪里可以生成数据库对应的Model,不用再自己手写Model.这里记录的就是如何从已建立好的数据库生成项目代 ...
- Entity Framework 6 Code First +MVC5+MySql/Oracle使用过程中的几个问题
1. namespace Snapsia.Web.Models { using System; using System.Data.Entity; using System.ComponentMode ...
- Entity Framework插入中文数据到MySQL乱码问题
1.MYSQL: 保证所有的的列都是UTF8格式. 2.在连接MySQL的连接字符串中,加入配置文件信息:Character Set=utf8
- 了解Entity Framework中事务处理
Entity Framework 6以前,框架本身并没有提供显式的事务处理方案,在EF6中提供了事务处理的API. 所有版本的EF,只要你调用SaveChanges方法进行插入.修改或删除,EF框架会 ...
- 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 ( ...
- ASP.NET没有魔法——ASP.NET MVC 与数据库之Entity Framework Migrations
在开发数据库应用程序的时候,经常会遇到某些表需要添加字段或者修改类型.新增表等需求,而对于EF Code First来说关注的只有实体类,当需求变更时只需要添加新的实体类或者在实体类中添加.删除.修改 ...
随机推荐
- Linux运维笔记(一)网络基础知识
网络基础知识 一.基本概念 1.ARPANET & TCP/IP:以“软件”技术将网络硬件整合,使得不同的计算机或者数据可以通过这个软件达成数据沟通(TCP/IP技术也被称为Internet) ...
- java参数转换为javaBean对象
package com.tuxin.tool.json; import flexjson.JSONDeserializer;import flexjson.JSONSerializer;import ...
- Servlet中的过滤器
在web.xml中配置:(用eclipse工具,可以在创建filter的时选择,web.xml中的配置可以自动生成) <filter> <display-name>LoginF ...
- bootStrap中的ul导航
<!doctype html><html > <head> <meta charset="utf-8"> <link rel= ...
- Installing Windows Features without Internet
To set the source file path for the sxs folder, complete these steps: Ensure you have the correct in ...
- js金额数字格式化实现代码(三位加逗号处理保留两位置小数)
工作中很常用的东西: 例1,使数字1111111变成11,111,111.00,保留两位小数. <html> <head> <script type="text ...
- 使用JDBC在MySQL数据库中快速批量插入数据
使用JDBC连接MySQL数据库进行数据插入的时候,特别是大批量数据连续插入(10W+),如何提高效率呢? 在JDBC编程接口中Statement 有两个方法特别值得注意: void addBatch ...
- vmware虚拟机环境下配置centos为静态IP的步骤
一.修改centos配置文件 配置文件位于/etc/sysconfig/network-scripts/ifcfg-eth0 修改的内容为: ONBOOT=yes BOOTPROTO=static I ...
- akka actors默认邮箱介绍
1. UnboundedMailbox is the default unbounded MailboxType used by Akka Actors ”无界邮箱“ 是akka actors默认使用 ...
- 外网IP监测上报程序(使用Poco库的SMTPClientSession发送邮件)
目录 IPReport 项目介绍 编译说明 安装使用说明 获取外网IP方式 邮件发送关键代码 IPReport 代码地址https://gitee.com/solym/IPReport 项目介绍 外网 ...