5

Sto riscontrando un po 'di difficoltà nell'ottenere Entity Framework 5 Enum per eseguire il mapping su una colonna intera in una migrazione. Ecco ciò che il codice è simile:Valore errato durante il salvataggio di enum

[Table("UserProfile")] 
public class UserProfile 
{ 
    public enum StudentStatusType 
    { 
     Student = 1, 
     Graduate = 2 
    } 

    [Key] 
    public int UserId { get; set; } 
    public string UserName { get; set; } 
    public string FullName { get; set; } 
    public StudentStatusType Status { get; set; } 
} 

La migrazione è simile al seguente:

public partial class EnumTest : DbMigration 
{ 
    public override void Up() 
    { 
     AddColumn("UserProfile", "Status", c => c.Int(nullable: false, defaultValue:1)); 
    } 

    public override void Down() 
    { 
     DropColumn("UserProfile", "Status"); 
    } 
} 

Tuttavia quando salvo cambiamenti non li rispecchia nel database.

var user = new UserProfile(); 
user.Status = UserProfile.StudentStatusType.Graduate; 
user.FullName = "new"; 
user.UserName = "new"; 
users.UserProfiles.Add(user); 
users.SaveChanges(); 

Database:

---------------------------------------------------- 
|UserId | UserName | FullName | Status | 
---------------------------------------------------- 
|1  | new  | new  | 1  | 
---------------------------------------------------- 
+0

Stai scegliendo .NET Framework 4 per caso e non .NET Framework 4.5? – Pawel

+0

Ho scelto come target .NET Framework 4.5. –

risposta

4

La ragione di questo è che l'enumerazione è annidato in una classe. Entity Framework non rileva tipi annidati. Prova a spostare l'enum dalla classe e verifica se funziona.

Modifica

EF6 ora supporta tipi nidificati (compresi enumerazioni) quando si utilizza codice Primo approccio.

+0

Era così. Grazie, non sapevo di questa restrizione. –

Problemi correlati