运行种子数据库

时间:2018-09-29 03:51:11

标签: c# entity-framework-core database-migration

如何在.NET Core中运行种子方法?

这是我的onModelBuilding方法:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    Users user = new Users
    {
        Id = Guid.NewGuid().ToString(),
        UserName = "system@admin.com",
        Email = "system@admin.com",
        FirstName = "System",
        LastName = "Administrator",
        CreatedOn = DateTime.Now,
        UpdatedOn = DateTime.Now
    };
    PasswordHasher<Users> passwordHasher = new PasswordHasher<Users>();
    user.PasswordHash = passwordHasher.HashPassword(user, "P@ssw0rd");

    modelBuilder.HasDefaultSchema(schema: DatabaseGlobals.SchemaName);
    modelBuilder.Entity<Users>().HasData(
        user    
    );
    modelBuilder.Entity<UserRoles>().HasKey(k=>new { k.UserId, k.RoleId});
    modelBuilder.Entity<Applications>();
    modelBuilder.Entity<Roles>();
    modelBuilder.Entity<EventLogs>();
    base.OnModelCreating(modelBuilder);
}

我试图运行Update-database,但是由于创建了我的表,它没有执行任何操作。

我的模型位于ClassLibrary中,但是没有Startup.cs文件

1 个答案:

答案 0 :(得分:1)

add-migration anyname

update-database