Closed kjanshair closed 7 years ago
@kjanshair how does the AdministratorSeedData
class and your ApplicationDbContext
look like? In particular I am interested in the constructors and the EnsureSeedData()
method, but there may be other relevant parts, e.g. do you have an OnConfiguring()
method on the DbContext
?
PS: you may want to check out how I fixed the code formating on your post.
Also, it may be obvious, but is the change of the database name in the connection string intentional?
@divega here is my EnsureSeedData() method
public async Task EnsureSeedData()
{
if (await _userManager.FindByEmailAsync("someone@someone.com") == null)
{
ApplicationUser administrator = new ApplicationUser
{
UserName = "someone@someone.com",
Email = "someone@someone.com"
};
await _roleManager.CreateAsync(new IdentityRole("Administrator"));
await _userManager.CreateAsync(administrator, "Password123!");
IdentityResult result = await _userManager.AddToRoleAsync(administrator, "Administrator");
}
}
and here is my Context class
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
:base(options)
{
}
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
}
}
Put this in Startup.cs file
using (var serviceScope = app.ApplicationServices.GetRequiredService<IServiceScopeFactory>().CreateScope())
{
serviceScope.ServiceProvider.GetService<ApplicationDbContext>().SeedUser();
}
Create a helper class like this and put everything you want to seed in it
public static async void SeedUser(this ApplicationDbContext context)
{
string[] roles = new string[] { "Administrator", "Manager", "Developer", "Accountant", "Designer", "Client", "Watcher" };
foreach (var role in roles)
{
var roleStore = new RoleStore<IdentityRole>(context);
//add default role to db
if (!context.Roles.Any(r => r.Name == role))
{
await roleStore.CreateAsync(new IdentityRole(role));
}
}
}
@ngohungphuc 's code should work
I've been using ASP.NET 5 RC1 in which I've an application in which I seed administrative credentials into the data base using Services as follows:
Here is the connection string sub-object:
"ConnectionString": "Server=.;Database=kjanshair;Trusted_Connection=True;"
And the service that I use to seed data is being seeded using the Administrative seed data. It all works fine but when I port it to RC2, with the following changes in the startup as:
with the connection string as:
"ConnectionString": "Server=.;Database=_CHANGE_ME;Trusted_Connection=True;"
I get an exception in Configure() method
saying:
Message = "Cannot open database \"_CHANGE_ME\" requested by the login. The login failed.\r\nLogin failed for user 'DESKTOP-55E4D9H\\Janshair Khan'."
Why Am I getting this error? I've looked all over the alternatives but couldn't resolved.
Sorry for not proper formatting the code.