user1238784 :
I have the following Migration configuration class:
namespace MVC_Authentication.Migrations
{
using Microsoft.AspNet.Identity.EntityFramework;
using MVC_Authentication.Models;
using System;
using System.Data.Entity;
using System.Data.Entity.Migrations;
using System.Linq;
using System.Threading.Tasks;
internal sealed class MigrationConfiguration : DbMigrationsConfiguration<ApplicationDbContext>
{
public MigrationConfiguration()
{
AutomaticMigrationsEnabled = true;
AutomaticMigrationDataLossAllowed = true;
ContextKey = "MVC_Authentication.Models.ApplicationDbContext";
}
protected override void Seed(ApplicationDbContext context)
{
// This method will be called after migrating to the latest version.
// You can use the DbSet<T>.AddOrUpdate() helper extension method
// to avoid creating duplicate seed data.
SeedDatabase(context).GetAwaiter().GetResult();
}
protected async Task SeedDatabase(ApplicationDbContext ctx)
{
var roleManager = new ApplicationRoleManager(new RoleStore<IdentityRole>(ctx));
if (await roleManager.FindByNameAsync("Administrator") == null)
await roleManager.CreateAsync(new IdentityRole("Administrator"));
if (await roleManager.FindByNameAsync("User") == null)
await roleManager.CreateAsync(new IdentityRole("User"));
ApplicationUserManager userManager = new ApplicationUserManager(new UserStore<ApplicationUser>(ctx));
var user_Admin = new ApplicationUser()
{
SecurityStamp = Guid.NewGuid().ToString(),
UserName = "Admin",
Email = "myEmail",
EmailConfirmed = true
};
if (await userManager.FindByNameAsync(user_Admin.UserName) == null)
{
await userManager.CreateAsync(user_Admin, "MyPassword");
await userManager.AddToRoleAsync(user_Admin.Id, "User");
await userManager.AddToRoleAsync(user_Admin.Id, "Administrator");
}
}
}
}
but when the execution arrives at:
if (await roleManager.FindByNameAsync("Administrator") == null)
the application locks and I can wait and wait. Maybe I should not use RoleManager and UserManager here? It's the only way to seed roles and users. Thanks for any hint of what possible can go wrong.
Jerdine Sabio :
Try the synchronous method in RoleManager instead of in ApplicationRoleManager;
var roleStore = new RoleStore<IdentityRole>(db);
var roleManager = new RoleManager<IdentityRole>(roleStore);
roleManager.FindName(...);
Guess you like
Origin http://10.200.1.11:23101/article/api/json?id=375726&siteId=1