UserManager CreateAsync上的连接错误

时间:2017-04-06 06:23:46

标签: mysql asp.net-core asp.net-identity entity-framework-core

尝试使用UserManager CreateAsync方法创建新用户时出现以下错误。我使用的是未经修改的IdentityUser和IdentityRole。我有一些DBSets填充了数据库中的数据,因此阅读不是问题,只是写入它似乎是。

    {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}

我正在使用;

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

错误中提到的第102行是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);
            }
        }

我正在学习asp.net而且我在这里磕磕绊绊地希望这是一个明显的错误,因为它似乎是一个基本的连接问题,但它没有意义,因为数据库可以使用IdentityDBContext但不是通过UserManager查询,在我的理解中使用IdentityDBContext连接。

1 个答案:

答案 0 :(得分:6)

请勿使用MySql.Data.EntityFrameworkCore-7.0.7-m61提供程序并使用其他内容。 Oracle设法让每个预发布版本都让自己难堪。

有了这个版本,他们设法让包裹变得如此困难,以至于它无法使用,因为该提供商似乎在每次查询后关闭它的连接。请改用Pomelo.EntityFrameworkCore.MySql 1.1.0(或等待Oracles提供商的下一个版本)。 Oracle在使用当前提供商版本

支持ADO.NET/EF方面从未发挥过强大作用

更好的是,使用PostgreSQL或SqlServer代替。