首页 文章

UserManager CreateAsync上的连接错误

提问于
浏览
2

尝试使用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实体框架核心7.0.7-m61

  • Visual Studio 2017

  • ASP.NET核心MVC 1.1.0

  • 身份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 回答

  • 6

    不要使用 MySql.Data.EntityFrameworkCore-7.0.7-m61 提供程序并使用其他内容 . Oracle设法在每个预发布版本中使自己难堪 .

    在这个版本中,他们设法让包裹变得如此困难,以至于每次查询后它都会连接起来 . 请改用 Pomelo.EntityFrameworkCore.MySql 1.1.0(或等待Oracles提供商的下一个版本) . Oracle在使用当前提供商版本支持ADO.NET/EF方面从未发挥过强大的作用

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

相关问题