C# 无法解析类型为';BackEnd.Models.UserContext';

C# 无法解析类型为';BackEnd.Models.UserContext';,c#,asp.net-core,asp.net-identity,entity-framework-core-2.2,C#,Asp.net Core,Asp.net Identity,Entity Framework Core 2.2,我是.NET Core应用程序中的新手,我尝试使用angulare和.NET Core创建应用程序,但当我想在我的应用程序中添加标识时,我遇到以下错误: 处理请求时发生未处理的异常。 InvalidOperationException:无法解析类型的服务 尝试激活时出现“BackEnd.Models.UserContext” 'Microsoft.AspNetCore.Identity.EntityFrameworkCore.UserStore9[Microsoft.AspNetCore.Ide

我是.NET Core应用程序中的新手,我尝试使用angulare和.NET Core创建应用程序,但当我想在我的应用程序中添加标识时,我遇到以下错误:

处理请求时发生未处理的异常。 InvalidOperationException:无法解析类型的服务 尝试激活时出现“BackEnd.Models.UserContext” 'Microsoft.AspNetCore.Identity.EntityFrameworkCore.UserStore
9[Microsoft.AspNetCore.Identity.IdentityUser,Microsoft.AspNetCore.Identity.IdentityRole,BackEnd.Models.UserContext,System.String,Microsoft.AspNetCore.Identity.IdentityUserClaim
1[System.String],Microsoft.AspNetCore.Identity.IdentityUserRole
1[System.String],Microsoft.AspNetCore.Identity.IdentityUserLogin
1[System.String],Microsoft.AspNetCore.Identity.IdentityUserToken
1[System.String],Microsoft.AspNetCore.Identity.IdentityRoleClaim
1[System.String]'

这是我的密码:

namespace BackEnd.Models
{
    public class UserContext: IdentityDbContext<IdentityUser>
    {
        public UserContext()
        {

        }

        public UserContext(DbContextOptions<UserContext> options): base(options)
        {

        }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            if (!optionsBuilder.IsConfigured)
            {
                IConfigurationRoot configuration = new ConfigurationBuilder()
                   .SetBasePath(Directory.GetCurrentDirectory())
                   .AddJsonFile("appsettings.json")
                   .Build();
                var connectionString = configuration.GetConnectionString("IdentityConnectionString");
                optionsBuilder.UseSqlServer(connectionString);
            }
        }

    }
}
namespace BackEnd.Models
{
公共类UserContext:IdentityDbContext
{
公共用户上下文()
{
}
公共用户上下文(DbContextOptions):基本(选项)
{
}
配置时受保护的覆盖无效(DBContextOptions Builder Options Builder)
{
如果(!optionBuilder.IsConfigured)
{
IConfigurationRoot配置=新配置生成器()
.SetBasePath(目录.GetCurrentDirectory())
.AddJsonFile(“appsettings.json”)
.Build();
var connectionString=configuration.GetConnectionString(“IdentityConnectionString”);
optionsBuilder.UseSqlServer(connectionString);
}
}
}
}
启动

namespace BackEnd
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);

            services.AddCors(options =>
            {
                options.AddPolicy("EnableCORS", builder =>
                {
                    builder.AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod().AllowCredentials().Build();
                });
            });

            services.AddIdentity<IdentityUser, IdentityRole>().AddEntityFrameworkStores<UserContext>();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                app.UseHsts();
            }

            app.UseHttpsRedirection();
            app.UseCors("EnableCORS");
            app.UseMvc();
        }
    }
}
命名空间后端
{
公营创业
{
公共启动(IConfiguration配置)
{
配置=配置;
}
公共IConfiguration配置{get;}
//此方法由运行时调用。请使用此方法将服务添加到容器中。
public void配置服务(IServiceCollection服务)
{
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
services.AddCors(选项=>
{
options.AddPolicy(“EnableCORS”,builder=>
{
builder.AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod().AllowCredentials().Build();
});
});
services.AddIdentity().AddEntityFrameworkStores();
}
//此方法由运行时调用。请使用此方法配置HTTP请求管道。
公共无效配置(IApplicationBuilder应用程序,IHostingEnvironment环境)
{
if(env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
其他的
{
//默认的HSTS值为30天。您可能希望在生产场景中更改此值,请参阅https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
应用程序UseCors(“EnableCORS”);
app.UseMvc();
}
}
}

您是否未添加默认令牌提供程序? 像这样改变

namespace BackEnd.Models
{
    // Add IdentityRole
    public class UserContext: IdentityDbContext<IdentityUser,IdentityRole>
    {
        public UserContext()
        {

        }
      ...
}

您是否未添加默认令牌提供程序? 像这样改变

namespace BackEnd.Models
{
    // Add IdentityRole
    public class UserContext: IdentityDbContext<IdentityUser,IdentityRole>
    {
        public UserContext()
        {

        }
      ...
}

忘记调用
AddDbContext
?另外,在
onconfiguration
中构建
new ConfigurationBuilder()
是错误的,它的同步api、json文件将在每次解析时被调用(如果您的上下文是默认的,则意味着每次请求时都会被调用。为什么不在
services.AddDbContext(config=>…)中设置提供程序呢
?您已经在那里获得了配置生成器,请参见忘记调用
AddDbContext
?P.S.Building
new ConfigurationBuilder()
OnConfiguration
中,它的同步api不好,每次解析时都会调用json文件(意味着在每个请求上,如果您的上下文的作用域是默认的。为什么不在
services.AddDbContext(config=>…)
中设置提供程序呢?您已经在那里获得了配置生成器,请参阅
 public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
            app.UseHsts();
        }

        app.UseHttpsRedirection();
        app.UseCors("EnableCORS");
        //add this
        app.UseAuthentication();
        app.UseMvc();
    }