简体   繁体   中英

ASP.NET Identity DB Seed

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?

问题是正在初始化的DbContext与我在ApplicationUser中使用的DbContext不同,因此它无法正常工作。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM