Code First EF:在数据库生成期间未将Enum Id添加到数据库

时间:2014-02-19 09:43:47

标签: c# asp.net-mvc entity-framework ef-code-first

我刚刚更改了我的数据模型,当数据库首次由EF生成时,我希望在数据表中有一些默认项。以前,我是通过种子方法做到的。但现在我发现它会更有用,我必须写的代码量会更少如果我使用枚举(默认值)。

这是我添加enum

后的当前模型
public partial class License
{     
    public int LicenseId { get; set; }
    public LicenseTypes Licensetype { get; set; }
}

public enum LicenseTypes
{
    Trial = 0,
    Paid = 1
}

这是我的DbContext班级

public partial class InfoBridgeSmartDatePickerDbContext : IdentityDbContext
{
    public InfoBridgeSmartDatePickerDbContext():base("DefaultConnection")
    {
    }

    public DbSet<License> Licenses { get; set; }

}

当我运行应用程序时,会生成数据库,但表Licenses不包含valuesId enum。它不应该持有枚举的valuesId吗?

我使用的 EF版是:6.0.2

2 个答案:

答案 0 :(得分:0)

不,数据库中没有枚举值。 EF只管理从原语到枚举的转换,在数据库级别没有约束。

答案 1 :(得分:-1)

尝试使用

public partial class License
{     
    public int LicenseId { get; set; }
    [NotMapped]
    public LicenseTypes Licensetype { get; set;{ _default = value; } }
}

我用过的是单值,对于你曾经检查过的对象..这就是诀窍