Tinyint(byte),SmallInt(Int16)与EF5中的Enum不兼容 [英] Tinyint(byte),SmallInt(Int16) not compatible with Enum in EF5
问题描述
使用数据库第一个设计并具有tinyint(或smallint)列:
Using Database first design and having tinyint (or smallint) column:
[MyEnumColumn] [tinyint] NOT NULL
我使用
External Type: NSpace.MyEnumType
Name:MyEnumType
UnderlyingType:Byte
NSpace.MyEnumType的定义如下:
Where NSpace.MyEnumType is defined like this:
public enum MyEnumType
{ One, Two, Three, All }
仅当尝试从上下文加载实体时出现此错误:
Only to get this error when trying to load entity from context:
指定的架构无效。错误:
Schema specified is not valid. Errors:
对于概念性
类型'EntityDataModel.MyEnumType'找不到对应的对象层类型。
No corresponding object layer type could be found for the conceptual type 'EntityDataModel.MyEnumType'.
以下信息对于解决先前的
错误可能有用:
The following information may be useful in resolving the previous error:
CLR枚举类型的基础类型与
不匹配EDM枚举类型的基础类型。
The underlying type of CLR enumeration type does not match the underlying type of EDM enumeration type.
如果我使用[Smallint]和[Int16],同样适用,但是一旦我将数据库更改为[Int ]并将枚举类型键入[Int32],该错误消失了。
Same applies if I use [Smallint] and [Int16] but once I change database to [Int] and enum type to [Int32] the error is gone.
为什么在以下情况下我需要将枚举值存储在4Byte(Int)数据字段中而不是1Byte(Tinyint)中99.9%的时间中的枚举没有超过256个项目,还是我还缺少其他东西?
Why do I need to store enum value in 4Byte (Int) data field instead of 1Byte (Tinyint) when enums in 99.9% time don't have more than 256 items or am I missing something else?
推荐答案
如果有人感兴趣问题出在枚举的默认类型中:
Well if anyone is interested the problem is in enum's default type:
public enum MyEnumType
{ One, Two, Three, All }
由于枚举默认为 int ,[基本类型:{字节}]与[外部类型] {MyEnumType:Int}的类型不匹配,因此要针对我的原始tinyint字段进行修复,您需要像这样定义枚举:
Since enum defaults to type int, [Underlying Type:{Byte}] doesn't match type of [External Type] {MyEnumType:Int} so to fix it for my original tinyint field you need to define your enum like this:
public enum MyEnumType : byte
{ One, Two, Three, All }
这篇关于Tinyint(byte),SmallInt(Int16)与EF5中的Enum不兼容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!