首页 > 解决方案 > 无法从脚手架创建新帐户

问题描述

我正在尝试使用新的 .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();
            });
        }
    }
}

标签: c#asp.net-identityentity-framework-corescaffolding

解决方案


此错误通常表明您的数据库中没有 AspNetUsers 表。您是否运行了脚手架中提供的迁移?这可以通过 powershell 或控制台终端来完成。导航到 Web 项目并Update-Database在使用 powershell 或dotnet ef database update控制台时执行。 https://docs.microsoft.com/en-us/ef/core/managing-schemas/migrations/


推荐阅读