5

移行時に Entity Framework 5 列挙型を整数列にマップするのに少し苦労しています。コードは次のようになります。

[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; }
}

移行は次のようになります。

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");
    }
}

ただし、変更を保存してもデータベースに反映されません。

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

データベース:

----------------------------------------------------
|UserId   |   UserName   |   FullName   |   Status |
----------------------------------------------------
|1        |   new        |   new        |   1      |
----------------------------------------------------
4

1 に答える 1

4

これは、列挙型がクラスにネストされているためです。Entity Framework は、入れ子になった型を検出しません。列挙型をクラスの外に移動してみて、それが機能するかどうかを確認してください。

編集

EF6 は、Code First アプローチを使用する場合に、入れ子になった型 (列挙型を含む) をサポートするようになりました。

于 2013-01-03T14:24:37.620 に答える