gpt4 book ai didi

mysql - UserManager CreateAsync 上的连接错误

转载 作者:行者123 更新时间:2023-11-29 01:20:00 43 4
gpt4 key购买 nike

尝试使用 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 核心 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 连接。

最佳答案

不要使用 MySql.Data.EntityFrameworkCore-7.0.7-m61 提供程序并使用其他东西。 Oracle 设法让它的每个预发布版本都让自己难堪。

在这个版本中,他们成功地阻止了包,以至于它无法使用,因为该提供商似乎在每次查询后都会关闭它的连接。请改用 Pomelo.EntityFrameworkCore.MySql 1.1.0(或等待 Oracles 提供程序的下一个版本)。 Oracle 在使用当前提供程序版本支持 ADO.NET/EF 方面向来不强

更好的是,改用 PostgreSQL 或 SqlServer。

关于mysql - UserManager CreateAsync 上的连接错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43247275/

43 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com