【发布时间】:2019-05-06 07:27:42
【问题描述】:
我是 EF Core 的新手,正在尝试播种枚举。
根据Data Seeding,此功能是 EF Core 2.1 的新增功能。
我查看了几个解决方案,包括这个 SO solution by Blake Mumford,但这对我不起作用,因为枚举不是引用类型。
我的目标(在迁移的帮助下)是让 Category 枚举被填充到一个名为 Category 的新 SQL 表中,并让我的 Payment 表包含一个将 Category 表作为外键引用的列。
任何帮助将不胜感激。谢谢。
public partial class Payment
{
public int PaymentId { get; set; }
public DateTime PostedDate { get; set; }
public string Vendor { get; set; }
public decimal Amount { get; set; }
public virtual Category Category { get; set; }
}
public enum Category
{
Restaurants,
Groceries,
[Display(Name = "Home Goods")]
HomeGoods,
Entertainment
}
public partial class MyDbContext : DbContext
{
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
// Error: The type Category must be a reference type in order to use it as parameter TEntity in the
// genric type or method ModelBuilder.Entity<TEntity>()
modelBuilder.Entity<Category>().HasData(Category.Restaurants,
Category.Groceries,
Category.HomeGoods,
Category.Entertainment);
}
}
【问题讨论】:
标签: asp.net entity-framework asp.net-core .net-core entity-framework-core