Yoa*_*oav 6 c# asp.net-mvc entity-framework asp.net-mvc-5 asp.net-identity-2
我创建了一个新的ASP.NET MVC-5应用程序,Individual User Accounts然后更新Nuget packages了解决方案中的所有内容.现在我试图遵循一些教程中显示的一些指导原则,但我遇到了一些问题.第一个是ApplicationRoleManager未创建在整个应用程序中使用的类(ApplicationUserManager已创建).第二个问题更多的是Entity-Framework:我已经看到,为了向用户和角色播种数据库,许多人在ApplicationDbContext类中创建了一个静态构造函数:
static ApplicationDbContext()
{
Database.SetInitializer<ApplicationDbContext>(new ApplicationDbInitializer());
}
Run Code Online (Sandbox Code Playgroud)
所以我添加了它,并且实现的ApplicationDbInitializer是:
public class ApplicationDbInitializer : DropCreateDatabaseIfModelChanges<ApplicationDbContext>
{
protected override void Seed(ApplicationDbContext context)
{
InitializeIdentityForEF(context);
base.Seed(context);
}
//Create User=Admin@Admin.com with password=Admin@123456 in the Admin role
public static void InitializeIdentityForEF(ApplicationDbContext db)
{
var userManager = HttpContext.Current.GetOwinContext().GetUserManager<ApplicationUserManager>();
var roleManager = HttpContext.Current.GetOwinContext().Get<ApplicationRoleManager>();
const string name = "admin@admin.com";
const string password = "Admin@123456";
const string roleName = "Admin";
//Create Role Admin if it does not exist
var role = roleManager.FindByName(roleName);
if (role == null)
{
role = new IdentityRole(roleName);
var roleresult = roleManager.Create(role);
}
var user = userManager.FindByName(name);
if (user == null)
{
user = new ApplicationUser { UserName = name, Email = name };
var result = userManager.Create(user, password);
result = userManager.SetLockoutEnabled(user.Id, false);
}
// Add user admin to Role Admin if not already added
var rolesForUser = userManager.GetRoles(user.Id);
if (!rolesForUser.Contains(role.Name))
{
var result = userManager.AddToRole(user.Id, role.Name);
}
}
Run Code Online (Sandbox Code Playgroud)
添加完所有内容后,我打开Package Manager Console并输入Enable-Migrations,然后Add-Migration someName再打开Update-Database.结果是数据库已成功创建,但没有数据插入数据库.
在注意到未插入数据后,我将Seed逻辑移动到主控制器的Index方法,并在运行应用程序后插入数据.我还需要将这一行添加 app.CreatePerOwinContext<ApplicationRoleManager>(ApplicationRoleManager.Create);
到Startup.Auth.cs文件中.
所以我的问题是:
ApplicationRoleManager手动输入课程吗? seed方法有效?更新
我已将Seed方法更改为:
protected override void Seed(ApplicationDbContext context)
{
var userManager = HttpContext.Current.GetOwinContext().GetUserManager<ApplicationUserManager>();
//since there is no ApplicationRoleManager (why is that?) this is how i create it
var roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(context));
const string name = "admin@admin.com";
const string password = "Admin@123456";
const string roleName = "Admin";
//Create Role Admin if it does not exist
var role = roleManager.FindByName(roleName);
if (role == null)
{
role = new IdentityRole(roleName);
var roleresult = roleManager.Create(role);
}
//app hangs here...
var user = userManager.FindByName(name);
if (user == null)
{
user = new ApplicationUser { UserName = name, Email = name };
var result = userManager.Create(user, password);
result = userManager.SetLockoutEnabled(user.Id, false);
}
// Add user admin to Role Admin if not already added
var rolesForUser = userManager.GetRoles(user.Id);
if (!rolesForUser.Contains(role.Name))
{
var result = userManager.AddToRole(user.Id, role.Name);
}
base.Seed(context);
}
Run Code Online (Sandbox Code Playgroud)
所以现在,Admin角色已经创建,但是当到达var user = userManager.FindByName(name);应用程序时,挂起时没有异常或任何消息......
使用迁移时,您可以使用内置初始化程序和Seed方法:
Database.SetInitializer<ApplicationDbContext>(new
MigrateDatabaseToLatestVersion<ApplicationDbContext,
APPLICATION.Migrations.Configuration>());
Run Code Online (Sandbox Code Playgroud)
和APPLICATION.Migrations.Configuration(这是由Enable-Migrations命令创建的):
protected override void Seed(ApplicationDbContext context)
{
// seed logic
}
Run Code Online (Sandbox Code Playgroud)
作为角色管理器,您还可以使用RoleManager<ApplicationRole>基本实现.
| 归档时间: |
|
| 查看次数: |
8252 次 |
| 最近记录: |