'Unable to seed users and roles after a new database is created in .Net 5
Once the database is created I am unable to get the users and roles to seed. I am using a multi-tenancy app that needs to create a database first and then seed the user and roles.
I do not get any error messages. The code completes however nothing is put into the database.
Main api
public class systemController : ControllerBase
{
private readonly SignInManager<User> _signInManager;
private readonly UserManager<User> _userManager;
private readonly RoleManager<Role> _roleManager;
private IServiceProvider _serviceProvider;
private readonly IMapper _mapper;
private readonly IUserRepository _repo;
public systemController(UserManager<User> userManager, SignInManager<User> signInManager,
RoleManager<Role> roleManager, IServiceProvider serviceProvider, IMapper mapper, IUserRepository repo)
{
_userManager = userManager;
_signInManager = signInManager;
_roleManager = roleManager;
_serviceProvider = serviceProvider;
_mapper = mapper;
_repo = repo;
}
//create the new database
[HttpGet("CreateNewDb/{dbName}")]
public ActionResult CreateNewDb(string dbName)
{
var __roleManager = _serviceProvider.GetRequiredService<RoleManager<Role>>();
var __userManager = _serviceProvider.GetRequiredService<UserManager<User>>();
string serverDataString = "server=localhost; user=user; password=password; database=";
// this method works and created the database
createDb(dbName, serverDataString);
// this method does not work
createUserRole(dbName, serverDataString, __roleManager, __userManager);
}
createUserRole method
private static void createUserRole(string dbName, string serverDataString, RoleManager<Role> __roleManager, UserManager<User> __userManager)
{
var connectionString = serverDataString.Replace("database=", "database=" + dbName);
// build a DbContext for this tenant
var optionsBuilder = new DbContextOptionsBuilder<DataContext>()
.UseMySql(connectionString,
new MySqlServerVersion(new Version(8, 0, 21)));
DataContext context = new DataContext(optionsBuilder.Options);
Seed.SeedRoles(context, __roleManager);
Seed.SeedUsers(context, __userManager);
}
role in seed class
public class Seed
{
public static void SeedRoles(DataContext context, RoleManager<Role> roleManager)
{
if (!context.Roles.Any())
{
//create roles
var roles = new List<Role>
{
new Role{Name = "Admin"},
new Role{Name = "Standard"},
new Role{Name = "Basic"}
};
foreach (var role in roles)
{
roleManager.CreateAsync(role).Wait();
}
}
}
Solution 1:[1]
I think you might don't add your roles to context and/or you don't commit changes in a right way. You can just do like this ,
public class Seed
{
public static async Task SeedRoles(DataContext context)
{
if (!context.Roles.Any())
{
//create roles
var roles = new List<Role>
{
new Role{Name = "Admin"},
new Role{Name = "Standard"},
new Role{Name = "Basic"}
};
context.AddRange(roles);
await context.SaveChangesAsync();
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
