Understanding MVC-5 Identity

柔情痞子 提交于 2019-12-02 21:20:12

When using migrations you can use the built in initializer and the Seed method:

Database.SetInitializer<ApplicationDbContext>(new 
    MigrateDatabaseToLatestVersion<ApplicationDbContext, 
    APPLICATION.Migrations.Configuration>());

and in APPLICATION.Migrations.Configuration (this was created by the Enable-Migrations command):

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

As a role manager you can also use the RoleManager<ApplicationRole> base implementation.

I also was a bit confused about hanging of application in this case. The problem can be solved in this way

var userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUserManager>(db));
var roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(db));

And for anyone using the ApplicationUser with Integer foreign key, the code is this one:

var userManager = new ApplicationUserManager(new ApplicationUserStore(context));
var roleManager = new ApplicationRoleManager(new ApplicationRoleStore(context));

This works great for default MVC 5 project.

var manager = new ApplicationUserManager(new UserStore<ApplicationUser>(context));

It doesn't appear that the solutions posted address the issue of the app hanging on the call of userManager.FindByName(name). I'm running into the same problem. It worked a few hours ago on my local. I published to Azure and it started hanging. When I tested my local again it all of a sudden started hanging at that step. No error is returned and no timeout (at least after waiting 10-15 minutes). Does anyone have any tips to address Yoav's ultimate question?

I have some other very simple seeding processes that run before adding roles, and db.Foo.AddOrUpdate(foo) calls are running without error, but not actually saving anything to the database.

I just spent a deeply unpleasant half day dealing with this. I finally managed to get the damn thing to fire:

public static void InitializeIdentityForEF(ApplicationDbContext context)
        {
            context.Configuration.LazyLoadingEnabled = true;

            //var userManager = HttpContext.Current
            //    .GetOwinContext().GetUserManager<ApplicationUserManager>();

            //var roleManager = HttpContext.Current
            //    .GetOwinContext().Get<ApplicationRoleManager>();

            var roleStore = new RoleStore<ApplicationRole, int, ApplicationUserRole>(context);
            var roleManager = new RoleManager<ApplicationRole, int>(roleStore);
            var userStore = new UserStore<ApplicationUser, ApplicationRole, int, ApplicationUserLogin, ApplicationUserRole, ApplicationUserClaim>(context);
            var userManager = new UserManager<ApplicationUser, int>(userStore);   
...

It's the end of an extremely long day, and I suspect someone's going to tell me why I shouldn't do this. The rest of my Seed method fires beautifully, however, using non-async methods (FindByName/Create).

user7596105

Sir goobering, You struggles have helped me get passed this problem, I had to do it a little different though.

   context.Configuration.LazyLoadingEnabled = true;

        //var userManager = HttpContext.Current.GetOwinContext().GetUserManager<ApplicationUserManager>();
        //var roleManager = HttpContext.Current.GetOwinContext().Get<ApplicationRoleManager>();

        const string name = "admin@example.com";
        const string password = "Admin@123456";
        const string roleName = "Admin";

        ***var userManager = new ApplicationUserManager(new UserStore<ApplicationUser>(context));
        var roleManager = new ApplicationRoleManager(new RoleStore<IdentityRole>(context));***

        //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);
        } 
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!