运行asp.net核心项目时如何播种新数据库并保留旧数据?

时间:2018-11-27 10:16:24

标签: .net asp.net-core

我有以下DatabaseInitializer。 enter image description here

public async Task SeedAsync() { 
    await _context.Database.MigrateAsync().ConfigureAwait(false);

    if (!await _context.Users.AnyAsync()) {
        _logger.LogInformation("Generating inbuilt accounts");

        const string adminRoleName = "administrator";
        const string userRoleName = "user";

        await EnsureRoleAsync(adminRoleName, "Default administrator", Permissions.GetAllPermissionValues());
        await EnsureRoleAsync(userRoleName, "Default user", new string[] { });

        await CreateUserAsync("admin", "Abc@123abc", "Male", "admin@gmail.com", "+1 (123) 000-0000", DateTime.Now, new string[] { adminRoleName });
        await CreateUserAsync("user", "Abc@123abc", "Female", "user@gmail.com", "+1 (123) 000-0001", DateTime.Now, new string[] { userRoleName });

        _logger.LogInformation("Inbuilt account generation completed");
    }
}

在这种情况下,我仅在数据库是新数据库并且没有任何用户和角色时添加新角色。但是我想播种新数据并保留旧数据。

种子项目将在我们启动项目时运行。

0 个答案:

没有答案