Search code examples
c#asp.net-mvcasp.net-identityasp.net-roles

Creating Users and Roles in MVC5


I am having some troubles trying to determine where the best place is to create users and roles for an MVC application. Essentially what I want to do is create an admin role and a user role that is already created BEFORE my application gets the the Home Controller.

Now the one thing I'm experiencing is that I added the proper code in my Initializer.cs file but when my application runs it goes to go home controller first and when I debug the code it never hits my Initializer.cs since at this point I haven't access anything regarding my DataContext. I was unsure exactly what code to include in this since it spans multiple files or if my description is enough for comprehension but I can include code if required.

To make it short and sweet, my goal is to create two roles and add users to the roles BEFORE it hits my HomeController. Thanks for any help offered !!


Solution

  • Okay so I found what I wanted to do. I think what you guys were trying to do was a bit more complicated then I needed to use.

    using (var context = new DataContext())
    {
        context.Database.Initialize(force: true);
    }
    

    I included this in my Global.asax.cs file inside the Application_Start() method and it did exactly what I was trying to accomplish. Hope this helps for anyone looking for something similar.