[RESOLVED]Suddenly began to hang during database initialization

Hello,

Suddenlybeginning
to hang at the database initialization here
roleManager.FindByName(roleAdministrator);

Rolling back to a previous version of the project did not help.

  • MVC5
  • EntityFramework 6

This
post

describes
the same problem.
But how
to solve it?

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 roleAdministrator = "Administrator";
            const string roleSuperUser = "SuperUser";
            const string roleUser = "User";

            var role1 = roleManager.FindByName(roleAdministrator); //hangs here
            if (role1 == null)
            {
                role1 = new IdentityRole(roleAdministrator);
                var roleresult = roleManager.Create(role1);
            }

            var role2 = roleManager.FindByName(roleSuperUser);
            if (role2 == null)
            {
                role2 = new IdentityRole(roleSuperUser);
                var roleresult = roleManager.Create(role2);
            }

            var role3 = roleManager.FindByName(roleUser);
            if (role3 == null)
            {
                role3 = new IdentityRole(roleUser);
                var roleresult = roleManager.Create(role3);
            }

            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);
            }

            var rolesForUser = userManager.GetRoles(user.Id);
            if (!rolesForUser.Contains(role1.Name))
            {
                var result = userManager.AddToRole(user.Id, role1.Name);
            }
        }

Help
solve the problem. Thank you!

 

Hi,

Don’t you have some kind of error message (possibly a timeout or whatever) if you wait long enough? Have you tried to see what happens database side (SQL Server Profiler, locks etc… not sure which db you are using).

Waited about
10
minutes.
No error
is displayed.
The database is created,
I can
view the tables,
but
the initialization
fails. Tested this
same
project on another
computer.
The problem is the same.

 

Hi DSerg,

Thanks for your post.

There’s a similar thread:

#Understanding MVC-5 Identity

http://stackoverflow.com/questions/25120942/understanding-mvc-5-identity

Maybe it will help you.

Best Regards,

Eileen

Hi all,

I’m having the same problem.  After working well for several straight weeks of development, all of a sudden my app is hanging on "roleManager.FindByName(roleName)".  In fact, it worked a few days ago after publishing to Azure, then worked after VERY minor
changes on my local, then hangs after publishing to Azure, and then the same code hangs on my local.  All I did was publish.  No errors after waiting about 10-15 minutes.

Additionally, I have several very simple db.Foo.AddOrUpdate(foo) calls that run without error, but all of a sudden save nothing to the database.  These are simple tables with no constraints, just an autonumbered key and a string.

The linked solution doesn’t seem to address the hanging problem.  I know "all of a sudden" means little, since something has to actually be causing it, but I can’t for the life of me think of what I would’ve done.

Any new thoughts?

Leave a Reply