Identification Asp.Net Creating a Role in the Seed Method

I tried to add a new role to the seed method, but when I run the code, the browser continues to load and has no response. Using a debugger, it was discovered that the code hangs on the creation method.

I have no idea what is going on. Any help is appreciated.

Thank!

public class ApplicationDbContextInitializer : DropCreateDatabaseIfModelChanges<ApplicationDbContext> 
{
    protected override void Seed(ApplicationDbContext context)
    {
        var rm = new RoleManager<IdentityRole>(
            new RoleStore<IdentityRole>(new ApplicationDbContext()));
        var idResult = rm.Create(new IdentityRole("Admin"));

        base.Seed(context);
    }
}

Global.asax file:

public class MvcApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);

        Database.SetInitializer(new ApplicationDbContextInitializer());

        ApplicationDbContext db = new ApplicationDbContext();
        db.Users.FirstOrDefault();

    }
}

Following this article, seed data is well tolerated. But I would like to find a solution without using migration so that it crashes and creates a new database every time the model changes. http://typecastexception.com/post/2013/11/11/Extending-Identity-Accounts-and-Implementing-Role-Based-Authentication-in-ASPNET-MVC-5.aspx

+4
1

var rm = new RoleManager<IdentityRole>(
    new RoleStore<IdentityRole>(new ApplicationDbContext()));

, Seed(), ...

var rm = new RoleManager<IdentityRole>(
    new RoleStore<IdentityRole>(context));
+4

Source: https://habr.com/ru/post/1532264/


All Articles