c# – 了解MVC-5身份

我使用个人用户帐户创建了一个新的ASP.NET MVC-5应用程序,然后更新了解决方案中的所有Nuget包.现在我试图遵循一些教程中显示的一些指导原则,但我遇到了一些问题.
第一个是没有创建在整个应用程序中使用的名为ApplicationRoleManager的类(ApplicationUserManager已创建).
第二个问题更多的是关于Entity-Framework:我已经看到,为了用一个用户和角色为数据库播种,许多人在ApplicationDbContext类中创建一个静态构造函数:
static ApplicationDbContext()
    {
        Database.SetInitializer<ApplicationDbContext>(new ApplicationDbInitializer());
    }

所以我添加了它,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);
        }
    }

添加完所有内容后,我打开了包管理器控制台并键入了Enable-Migrations,然后添加了迁移someName,然后是Update-Database.
结果是数据库已成功创建,但没有数据插入数据库.
注意到未插入数据后,我将Seed逻辑移动到主控制器的Index方法,并在运行应用程序后插入数据.
我还需要添加以下行:app.CreatePerOwinContext< ApplicationRoleManager>(ApplicationRoleManager.Create);
到Startup.Auth.cs文件.
所以我的问题是:

>我真的需要进入ApplicationRoleManager类
手动?
>如何使种子方法有效?

UPDATE
我已将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,role.Name);
        }
        base.Seed(context);
    }

所以现在,创建了Admin角色,但是在获取var user = userManager.FindByName(name)时;应用程序挂起,没有例外或任何消息……

解决方法

使用迁移时,您可以使用内置初始化程序和Seed方法:
Database.SetInitializer<ApplicationDbContext>(new 
    MigrateDatabaseToLatestVersion<ApplicationDbContext,APPLICATION.Migrations.Configuration>());

在APPLICATION.Migrations.Configuration中(这是由Enable-Migrations命令创建的):

protected override void Seed(ApplicationDbContext context)
{
    // seed logic
}

作为角色管理器,您还可以使用RoleManager< ApplicationRole>基础实施.

相关文章

文章浏览阅读6.2k次,点赞2次,收藏3次。C#数学运算表达式解...
文章浏览阅读5.2k次,点赞6次,收藏7次。程序要做到用户配置...
文章浏览阅读9k次。错误信息检测到 ContextSwitchDeadlock M...
文章浏览阅读2w次,点赞10次,收藏9次。我发生错误时的环境:...
文章浏览阅读9.8k次。C# 二进制字节流查找函数IndexOf ...
文章浏览阅读2.5w次,点赞3次,收藏9次。c#DataGridView数据...