Reputation: 103
I tried to add a new role in the seed method, but when i run the code, the browser keep loading and have no response. Using the debugger, it was found that the code hanged on the create method.
I have no idea what's going.. Any help is appreciated .
Thanks !
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, Seeding data is fine with migration. But i would like to find a solution without using migration so that it will drop and create a new database everytime the model changes. http://typecastexception.com/post/2013/11/11/Extending-Identity-Accounts-and-Implementing-Role-Based-Authentication-in-ASPNET-MVC-5.aspx
Upvotes: 4
Views: 1474
Reputation: 37540
Try changing
var rm = new RoleManager<IdentityRole>(
new RoleStore<IdentityRole>(new ApplicationDbContext()));
to use the context that was passed into the Seed()
method, rather than creating a new one...
var rm = new RoleManager<IdentityRole>(
new RoleStore<IdentityRole>(context));
Upvotes: 4