User manager CreateAsync running very slow - Identity 2

asp.net-identity-2 asp.net-mvc-5 c# entity-framework-6

Question

I'm running this query to import uses from an old MySQL database to a new SQL database using entity framework and Microsoft's Identity 2 and on the local server Visual Studio Community edition runs alongside SQL Server 10.0.5

User manager declaration

private ApplicationUserManager _userManager;
public ApplicationUserManager UserManager
{
    get
    {
        if (_userManager == null)
        {
            this._userManager = HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>();
            this._userManager.PasswordValidator = new CustomPasswordValidator();
        }
        return _userManager;
    }
    private set
    {
        _userManager = value;
    }
}

Controller methods - the sql and MySQL unit of work are in the controller construct, only 1 context per db is made for this entire process

public async Task<ActionResult> Index()
{
    await ImportUsers();

    return View();
}

private async Task<bool> ImportUsers()
{
    DateTime timeNow = DateTime.Now;

    // Returns a full list of old users in about a millisecond
    List<default_users> users = 
        this.mySQLUnitOfWork
        .MySQLRepo
        .GetUsers()
        .ToList(); 

    foreach (var user in users)
    {                  
        if (user.username != null && user.email != null &&
            user.username != "" && user.email != "" &&
            UserManager.FindByEmail(user.email) == null)
        {
            default_profiles profile = 
                this.mySQLUnitOfWork
                    .MySQLRepo
                    .GetProfiles()
                    .Where(x => 
                        x.id == user.id
                    ).FirstOrDefault();

            if (profile != null)
            {          
                var newUser = new ApplicationUser { 
                    UserName = user.username.ToLower(), 
                    Email = user.email.ToLower(), 
                    CreatedAt = timeNow, 
                    UpdatedAt = timeNow 
                };

                newUser.Profile = new UserProfile { 
                    CreatedAt = timeNow, 
                    UpdatedAt = timeNow, 
                    FirstName = profile.first_name, 
                    LastName = profile.last_name, 
                    User = newUser 
                };

                // Takes ages to complete
                var result = await UserManager.CreateAsync(
                    newUser, 
                    Guid.NewGuid().ToString("N")
                );

                if (result.Succeeded)
                {
                    UserManager.AddToRole(newUser.Id, RoleChoices.MEMBER);             
                    var dbUser = this.sqlUnitOfWork.UserRepository.GetByID(newUser.Id);
                }
            }
        }
    }

    return true;
}

Problem:

This operation takes at least 45 minutes for 2000 user records. It seems that the CreateAsync and AddToRole methods take absolutely ages to run. Any reasons why this might be?

Note:

I have been debugging this to death... The slowest part is waiting for the CreateAsync method to complete... It's really quite odd? It takes atleast 1 second to create a user.

1
1
5/19/2015 9:07:58 AM

Popular Answer

It seems you are not destroying your Context/Repository, if you keep your repository alive for entire application scope, it will grow in size. It is bad practice, usually repository should only live along with request life cycle, it should be created when new request starts and it will be disposed when request finishes.

1
5/8/2015 2:22:17 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow