2
votes

I am getting the following error when trying to create a new user using the UserManager CreateAsync method. I am using an unmodified IdentityUser and IdentityRole. I have a few DBSets which populate with data from the database, so reading is not an issue, just writing to it seems to be.

    {System.InvalidOperationException: Connection must be valid and open to commit transaction at MySql.Data.MySqlClient.MySqlTransaction.Commit()
   at Microsoft.EntityFrameworkCore.Storage.RelationalTransaction.Commit()
   at Microsoft.EntityFrameworkCore.Update.Internal.BatchExecutor.<ExecuteAsync>d__10.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.<SaveChangesAsync>d__54.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.<SaveChangesAsync>d__52.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.EntityFrameworkCore.DbContext.<SaveChangesAsync>d__35.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.Identity.EntityFrameworkCore.UserStore`9.<CreateAsync>d__36.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.Identity.UserManager`1.<CreateAsync>d__68.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.Identity.UserManager`1.<CreateAsync>d__73.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at Controllers.Auth.AuthController.<Register>d__9.MoveNext() in AuthController.cs:line 102}

I'm using;

  • MySQL Entity Framework Core 7.0.7-m61
  • Visual Studio 2017
  • ASP.NET Core MVC 1.1.0
  • Identity 1.1

Line 102 mentioned in the error is var result = await userManager.CreateAsync(newUser, model.Password);

        [HttpPost]
        public async Task<ActionResult>Register(RegisterViewModel model)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    var newUser = new IdentityUser
                    {
                        Email = model.Email,
                        UserName = model.Username
                    };

                    var result = await _userManager.CreateAsync(newUser, model.Password);

                    if (result.Succeeded)
                    {
                        await _signInManager.SignInAsync(newUser, false);
                        _logger.LogInformation($"New user account created: {model.Username}");

                        return RedirectToAction("Index", "App");
                    }

                    _logger.LogError($"Registration failed for {model.Username}: ", result.Errors);

                    return View(model);
                }

                return View(model);
            }
            catch (Exception ex)
            {
                _logger.LogError("Database Failure: ", ex);
                return View(model);
            }
        }

I'm learning asp.net and am stumbling here on what I'm hoping is an obvious error as it seems to be a basic connection issue but it's not making sense as the database can be queried using IdentityDBContext but not through the UserManager, which in my understanding uses the IdentityDBContext connection.

1
Also please note: There is no MVC6 and no Identity 3! This outdated terms will only bring confusion and all the packages have been renamed over 18 (!!) months ago. Both ASP.NET Core MVC and ASP.NET Core Identity were renamed (Adding the Core to the name and resetting the version to 1.0) with rc2 release of ASP.NET Core to make it obvious it's a complete rewrite and in no way compatible with the previous version of MVC and IdentityTseng

1 Answers

6
votes

Do not use MySql.Data.EntityFrameworkCore-7.0.7-m61 provider and use something else. Oracle manages to embarrass themselves with each pre-release version of it.

With this release they managed to brake the package so hard, that it's unusable as that provider seems to close it's connection after every query. Use Pomelo.EntityFrameworkCore.MySql 1.1.0 instead (or wait for the next release of Oracles provider). Oracle was never strong at supporting ADO.NET/EF with current provider versions

Even better, use PostgreSQL or SqlServer instead.