我正在尝试为我的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());
}
}
任何可能出错的想法?
答案 0 :(得分:3)
问题在于,正在初始化的DbContext与我在ApplicationUser中使用的DbContext不同,因此它无法正常工作。