C# .Net Core 2.1似乎忽略了modelBuilder.Entity<;张力>;()HasData

C# .Net Core 2.1似乎忽略了modelBuilder.Entity<;张力>;()HasData,c#,entity-framework,enums,.net-core,entity-framework-core,C#,Entity Framework,Enums,.net Core,Entity Framework Core,我有一个.net核心项目,包含模型、服务和api项目。我在这个项目中的最初目标是使枚举以更“代码优先”的方式工作。 我只希望能够修改枚举,并相应地更新数据库和查找表。在花了太多时间试图弄清楚如何做到这一点后,我决定创建一个特殊的枚举类,并使用“HasData”手动为其种子。我以前做过这件事,从来没有遇到过问题。我上一次做这个项目是在.NETCore2.1。这是使用2.2。我不知道我是否发现了一个奇怪的bug或者我只是做错了什么。我的“服务”项目是DbContext所在的地方,大部分相关代码都在这

我有一个.net核心项目,包含模型、服务和api项目。我在这个项目中的最初目标是使枚举以更“代码优先”的方式工作。 我只希望能够修改枚举,并相应地更新数据库和查找表。在花了太多时间试图弄清楚如何做到这一点后,我决定创建一个特殊的枚举类,并使用“HasData”手动为其种子。我以前做过这件事,从来没有遇到过问题。我上一次做这个项目是在.NETCore2.1。这是使用2.2。我不知道我是否发现了一个奇怪的bug或者我只是做错了什么。我的“服务”项目是DbContext所在的地方,大部分相关代码都在这里

我有一个模型,看起来像:

public class Employee : IEmployee
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    [Display(Name = "Id")]
    public int Id { get; set; }

    [StringLength(512, ErrorMessage = "The first name cannot be longer than 512 characters")]
    [Display(Name = "First Name")]
    [Required]
    public string FirstName { get; set; }

    [StringLength(512, ErrorMessage = "The last name cannot be longer than 512 characters")]
    [Display(Name = "Last Name")]
    [Required]
    public string LastName { get; set; }

    public Department Department { get; set; }

}
它有一个枚举:

public enum Department{

    [Description("Sales")]
    Sales = 1,

    [Description("Customer Service")]
    CustomerService = 2,

    [Description("Technical Support")]
    TechnicalSupport = 3
}
我创建了一个与枚举对应的模型,如下所示:

[Table("Department")]
public class DepartmentEnum : EnumBase<Department>
{
    public DepartmentEnum()
    {
    }
}
public class EnumBase<TEnum> where TEnum : struct
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public virtual int Id { get; set; }

    [Required]
    [MaxLength(100)]
    public virtual string Name { get; set; }

    [MaxLength(100)]
    public virtual string Description { get; set; }

    public virtual bool Deleted { get; set; } = false;
}
public class ApplicationDbContext : IdentityDbContext<AppUser>
{
    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options){}

    public DbSet<Employee> Employees { get; set; }

    public DbSet<DepartmentEnum> Departments { get; set; }

    protected internal virtual void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<DepartmentEnum>().HasData(
            new DepartmentEnum { Id = 1, Name = "Sales", Description = "Sales", Deleted = false },
            new DepartmentEnum { Id = 2, Name = "Customer Service", Description = "Customer Service", Deleted = false },
            new DepartmentEnum { Id = 3, Name = "TechnicalSupport", Description = "Technical Support", Deleted = false }
        );

    }
}
public class DbContextFactory : IDesignTimeDbContextFactory<ApplicationDbContext>
{
    public ApplicationDbContext CreateDbContext(string[] args)
    {
        //uncomment this line if you need to debug this code
        //then choose yes and create a new instance of visual
        //studio to step through the code
        //Debugger.Launch();

        var builder = new DbContextOptionsBuilder<ApplicationDbContext>();
        builder
            .UseSqlServer(@"Server=(localdb)\MSSQLLocalDB;Database=EnumSeeder;Trusted_Connection=True;MultipleActiveResultSets=true");

        //get the dbContext
        var context = new ApplicationDbContext(builder.Options);

        return context;
    }
}
当我运行
addmigration InitialCreate
时,我希望看到移植文件中反映的模型数据。当我查看部门表时,我看到的是:

migrationBuilder.CreateTable(
    name: "Department",
    columns: table => new
    {
        Id = table.Column<int>(nullable: false)
            .Annotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn),
        Name = table.Column<string>(maxLength: 100, nullable: false),
        Description = table.Column<string>(maxLength: 100, nullable: true),
        Deleted = table.Column<bool>(nullable: false)
    },
    constraints: table =>
    {
        table.PrimaryKey("PK_Department", x => x.Id);
    });
migrationBuilder.CreateTable(
名称:“部门”,
列:表=>new
{
Id=table.Column(可空:false)
.Annotation(“SqlServer:ValueGenerationStrategy”,SqlServerValueGenerationStrategy.IdentityColumn),
Name=table.Column(maxLength:100,null:false),
Description=table.Column(最大长度:100,可为空:true),
Deleted=table.Column(可空:false)
},
约束:表=>
{
表.PrimaryKey(“PK_部门”,x=>x.Id);
});
无论我做什么,造物的“HasData”药水永远不存在。 我已尝试将db上下文切换为普通的旧
DbContext
。我已经尝试通过先添加employee表,然后返回并添加department表作为第二次迁移来更改事件的顺序。似乎我所做的一切都不会让迁移包含种子数据


有人能发现我的问题吗?

您应该注意编译器警告

这里

您正在跟踪基本的
OnModelCreating
方法(必须有编译器警告告诉您这一点)。当然,EF核心基础设施并不是在调用您的方法,而是在
DbContext
类中定义的方法

因此,无论你在那里放什么都没有效果,
Department
表是根据EF核心约定创建的,当然没有数据种子

将方法签名更改为

protected override void OnModelCreating(ModelBuilder modelBuilder)
一定要打电话

base.OnModelCreating(modelBuilder);

一开始。当您继承
IdentityContext
(或除没有任何功能的
DbContext
以外的任何基本上下文类)时,这一点尤其重要。

好了,现在我必须去拿我的傻瓜帽,坐在角落里:)警告就在那里等着我读。它现在很有魅力。谢谢你的帮助!
base.OnModelCreating(modelBuilder);