4

我正在尝试为我的 MVC 5 Web 应用程序设置一些种子数据,但它似乎没有为 IdentityUser 创建任何种子数据。当我检查 App_Data 文件夹时,它是空的(启用了显示所有文件)

这是我的 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);
    }
}

和我的 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());
    }
}

任何想法可能出了什么问题?

4

1 回答 1

3

问题是正在初始化的 DbContext 与我在 ApplicationUser 中使用的不同,因此它为什么不起作用。

于 2013-11-03T11:27:13.773 回答