UserManager CreateAsync上的連接錯誤

asp.net-core asp.net-identity entity-framework-core mysql

嘗試使用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 Core 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連接。

一般承認的答案

不要使用MySql.Data.EntityFrameworkCore-7.0.7-m61提供程序並使用其他內容。 Oracle設法在每個預發布版本中使自己難堪。

在這個版本中,他們設法製造瞭如此困難的軟件包,它無法使用,因為該提供商似乎在每次查詢後關閉它的連接。改為使用Pomelo.EntityFrameworkCore.MySql 1.1.0(或等待Oracles提供商的下一個版本)。 Oracle在使用當前提供商版本支持ADO.NET/EF方面從未發揮過強大的作用

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



Related

許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow
許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow