Question

My MVC 5 Web Application needs some seed data, but it doesn't appear to be creating any for IdentityUser as I try to set it up. The App Data folder is empty when I inspect it (Show All files is enabled)

I've attached my WebAppDatabaseInitializer.cs file.

public class WebAppDatabaseInitializer : DropCreateDatabaseIfModelChanges<WebAppDbContext> 
{
    protected override void Seed(WebAppDbContext context)
    {
        var UserManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context));
        var RoleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(context));

        string name = "Admin";
        string password = "123456";
        string test = "test";

        //Create Role Test and User Test
        RoleManager.Create(new IdentityRole(test));
        UserManager.Create(new ApplicationUser() { UserName = test });

        //Create Role Admin if it does not exist
        if (!RoleManager.RoleExists(name))
        {
            var roleresult = RoleManager.Create(new IdentityRole(name));
        }

        //Create User=Admin with password=123456
        var user = new ApplicationUser();
        user.UserName = name;
        var adminresult = UserManager.Create(user, password);

        //Add User Admin to Role Admin
        if (adminresult.Succeeded)
        {
            var result = UserManager.AddToRole(user.Id, name);
        }
        base.Seed(context);
    }
}

as well as my Global.asax.cs

public class MvcApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();
        GlobalConfiguration.Configure(WebApiConfig.Register);
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);
        Database.SetInitializer(new WebAppDatabaseInitializer());
    }
}

Any suggestions as to what might be wrong?

1
4
11/2/2013 8:39:20 PM

Accepted Answer

The reason it wasn't working was because the DbContext that was initialized was different from the one I used in ApplicationUser.

3
11/4/2013 9:36:09 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow