保存枚举时值不正确

Jef*_*ear 5 c# entity-framework code-first ef-migrations entity-framework-5

我将Entity Framework 5 Enums映射到迁移中的整数列时遇到一些困难.这是代码的样子:

[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; }
}
Run Code Online (Sandbox Code Playgroud)

迁移看起来像这样:

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");
    }
}
Run Code Online (Sandbox Code Playgroud)

但是,当我保存更改时,它不会在数据库中反映它们.

var user = new UserProfile();
user.Status = UserProfile.StudentStatusType.Graduate;
user.FullName = "new";
user.UserName = "new";
users.UserProfiles.Add(user);
users.SaveChanges();
Run Code Online (Sandbox Code Playgroud)

数据库:

----------------------------------------------------
|UserId   |   UserName   |   FullName   |   Status |
----------------------------------------------------
|1        |   new        |   new        |   1      |
----------------------------------------------------
Run Code Online (Sandbox Code Playgroud)

Paw*_*wel 5

原因是枚举嵌套在类中。实体框架不会发现嵌套类型。尝试将枚举移出类,看看它是否有效。

编辑

使用 Code First 方法时,EF6 现在支持嵌套类型(包括枚举)。