实体框架:映射TINYINT布尔 [英] Entity Framework: mapping tinyint to boolean

查看:182
本文介绍了实体框架:映射TINYINT布尔的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

默认情况下,实体框架映射到TINYINT字节。



我试图改变基础类型它产生的布尔之后,但得到的编译错误



指定的成员映射无效。类型'Edm.Boolean [可空=假,默认值='成员等等...



这可能在4.0吗?



这不是我的主意,用TINYINT列布尔值。这是通过使用Hibernate这显然不这么认为对MySQL的兼容性另一支球队自动完成。显然TINYINT比2。我正在寻找一种方式来映射它使点儿接受1是假的,或任何接受0是真实的多个值。无论是将工作对我来说



有没有办法在各种各样的类型转换成EF堵塞?


解决方案
<关于整型p>从MSDN的页面,我们看到 TINYINT 类型代表整数范围从0到255



A 布尔,相比之下,仅代表一个二进制 0 1



字节更改从默认映射到布尔(如果它甚至有可能,它根据此页面看起来它是没有)没有任何意义 - 如何,例如,你所代表的价值 42 (有效 TINYINT )作为布尔



如果你需要一个实体类型 BOOL <的属性/ code>,我建议映射它的类型 <柱code>位


by default Entity Framework maps tinyint to byte.

i tried changing the underlying type after it was generated to Boolean, but getting compilation error

Member Mapping specified is not valid. The type 'Edm.Boolean[Nullable=False,DefaultValue=]' of member blah...

is this possible in 4.0?

it wasn't my idea to use tinyint column as boolean. this was done automatically by another team using hibernate which apparently does it that way for mysql compatibility. obviously tinyint has more values than 2. I am looking for a way to map it so that anyting accept for 1 is false, or anything accept for 0 is true. either would work for me

is there a way to plug in a type translator of sorts into EF?

解决方案

From MSDN's page on integer types, we see that the tinyint type represents an integer ranging from 0 to 255.

A bool, in contrast, represents only a binary 0 or 1.

Changing the default mapping from byte to bool (if it were even possible, which according to this page it seems like it's not) does not make sense -- how, for example, would you represent the value 42 (a valid tinyint) as a bool?

If you need an entity with a property of type bool, I'd suggest mapping it to a column of type bit.

这篇关于实体框架:映射TINYINT布尔的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆