Usually Admin Roles are seeded into the database while creating the schema. You are using Identity to create or seed the Admin Role. Identity uses Code-First approach so that you can customize it as much as possible. Don't use SQL queries manually. Below is way through which you can create an Admin User with Administrator privileges data at runtime using DI because EFCore does not yet support built-in support for Seeding data (It will support in the update).
To Seed Data, create a class (AdministratorSeedData.cs for example) in the Data Folder and add the following code.
public class AdministratorSeedData
{
private RoleManager<IdentityRole> _roleManager;
private UserManager<ApplicationUser> _userManager;
public AdministratorSeedData(UserManager<ApplicationUser> userManager, RoleManager<IdentityRole> roleManager)
{
_userManager = userManager;
_roleManager = roleManager;
}
public async Task EnsureSeedDataAsync()
{
if (await _userManager.FindByEmailAsync("someone@someone.com") == null)
{
ApplicationUser administrator = new ApplicationUser()
{
UserName = "someone@someone.com",
Email = "someone@someone.com"
};
await _userManager.CreateAsync(administrator, "Passw0rd123!");
await _roleManager.CreateAsync(new IdentityRole("Administrator"));
IdentityResult result = await _userManager.AddToRoleAsync(administrator, "Administrator");
}
}
}
Then in the Startup.cs
class, register this service in the DI container as:
public void ConfigureServices(IServiceCollection services)
{
....
services.AddTransient<AdministratorSeedData>();
}
and
public async void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, AdministratorSeedData seeder)
{
....
await seeder.EnsureSeedDataAsync();
}
Make sure that there is no database created. Run the application and you will see that the User with Administrator Role is created successfully.