我有以下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");
}
}
在这种情况下,我仅在数据库是新数据库并且没有任何用户和角色时添加新角色。但是我想播种新数据并保留旧数据。
种子项目将在我们启动项目时运行。