【发布时间】:2014-02-19 09:43:46
【问题描述】:
我刚刚更改了我的数据模型,当数据库第一次由 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 不包含 enum 的 values 或 Id。它不应该持有枚举的values 或Id 吗?
我使用的EF版本是:6.0.2
【问题讨论】:
-
表
License中确实应该有一个Licensetype int not null列;我认为您发布的代码没有问题 -
但是表格是否也会生成两行,Id 为 0,1,LicenseType 为 Trial 和 Paid,还是必须在种子方法期间将其添加到表格中?
-
目前我不知道表中会出现什么,只是表中包含空列(LicenseId 和 LicenseType)或包含一些值的列
-
可能我脑子里对这件事的工作原理有完全不同的看法。
-
不,您的代码中的
License是一个实体,并且需要一个LicenseTypes值作为其字段之一,并带有一个单独的LicenseId字段。您可以使用LicenseTypes作为实体的 PK,但您仍然需要为每个枚举值手动添加实体的实例
标签: c# asp.net-mvc entity-framework ef-code-first