我尝试使用新的 .Net Core 2.1 中的新身份脚手架从头开始创建授权和身份验证,但不断收到此错误。
SqlException:无效的对象名称“AspNetUsers”。
RegisterModel 无法在此行执行:
var result = await _userManager.CreateAsync(user, Input.Password);
IDENTITYHOSTINGSTARTUP.CS
[assembly: HostingStartup(typeof(Authorize.Areas.Identity.IdentityHostingStartup))]
namespace Authorize.Areas.Identity
{
public class IdentityHostingStartup : IHostingStartup
{
public void Configure(IWebHostBuilder builder)
{
builder.ConfigureServices((context, services) => {
services.AddDbContext<AuthorizeContext>(options =>
options.UseSqlServer(
context.Configuration.GetConnectionString("AuthorizeContextConnection")));
services.AddDefaultIdentity<AuthorizeUser>()
.AddEntityFrameworkStores<AuthorizeContext>()
.AddDefaultTokenProviders();
});
}
}
}
最佳答案
此错误通常表明您的数据库中没有 AspNetUsers 表。您是否运行了脚手架中提供的迁移?这可以通过 powershell 或控制台终端来完成。如果使用 powershell 或用于控制台的 dotnet ef database update
,导航到 Web 项目并执行 Update-Database
。
https://learn.microsoft.com/en-us/ef/core/managing-schemas/migrations/
关于c# - 无法从脚手架创建新帐户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50806778/