2012-11-10 3 views
22

Verwenden von Datenbank erstes Designs und mit Tinyint (oder smallint) Säule:Tinyint (Byte), SmallInt (Int16) nicht kompatibel mit Enum in EF5

[MyEnumColumn] [tinyint] NOT NULL 

ich diese Spalte Aufzählungstyp in EDM abgebildet mit

External Type: NSpace.MyEnumType 
Name:MyEnumType 
UnderlyingType:Byte 

Wo NSpace.MyEnumType wie folgt definiert ist:

public enum MyEnumType 
{ One, Two, Three, All } 

Nur um diesen Fehler zu erhalten, wenn Entität zu laden versuchen, von Kontext:

Schema specified is not valid. Errors:

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:

The underlying type of CLR enumeration type does not match the underlying type of EDM enumeration type.

Gleiche gilt, wenn ich [Smallint] und [Int16], aber sobald ich Datenbank [Int] und Aufzählungstyp [Int32] der Fehler ist weg ändern.

Warum muss ich enum Wert in 4Byte (Int) Datenfeld anstelle von 1Byte (Tinyint) speichern, wenn enums in 99,9% Zeit nicht mehr als 256 Elemente haben oder fehlt mir etwas anderes?

Antwort

63

Nun, wenn jemand interessiert ist das Problem in Standardtyp ENUM ist:

public enum MyEnumType 
{ One, Two, Three, All } 

Da ENUM defaults int eingeben, [Basiswert Typ: {Byte}] nicht mit Art von [ External] {MyEnumType: Int} so zu beheben, es für meine ursprüngliche Tinyint Feld, das Sie benötigen, um Ihre enum wie folgt zu definieren:

public enum MyEnumType : byte 
{ One, Two, Three, All } 
+0

dies auch den Fehler behoben, die ich heute hatte. So +1 – magicandre1981

+0

Das rettete meinen Tag :) +1 – Midas

+0

Nun, wenn jemand interessiert ist, ja sicher speichern Sie mich! Dank bro!! – CMS