Reputation: 1393
I am trying to set up some seed data for my MVC 5 Web Application, but it doesn't seem to be creating any for IdentityUser. When I check the App_Data folder it's empty (Show All files is enabled)
Here is my WebAppDatabaseInitializer.cs
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);
}
}
and 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 ideas what might be going wrong?
Upvotes: 4
Views: 9598
Reputation: 1393
The issue was that the DbContext that was being initialized was different that the one I used in ApplicationUser hence why it wasn't working.
Upvotes: 3