Asp.net 无法解析类型为';MyApp.ApplicationDbContext';试图激活时
在集成到现有ASP.NETCore2.0应用程序中后,我遇到了一个问题,即基于ASP.NETCore2.0的新创建的身份验证项目项 经过一些调整,然后成功构建,在浏览器中我得到了下一个错误: 处理请求时发生未处理的异常。 InvalidOperationException:尝试激活时无法解析类型为“SpaServices.ApplicationDbContext”的服务 身份验证必须使用尚未搭建的单独数据库 我的Startup.cs如下所示:Asp.net 无法解析类型为';MyApp.ApplicationDbContext';试图激活时,asp.net,authentication,asp.net-core,entity-framework-core,asp.net-core-2.0,Asp.net,Authentication,Asp.net Core,Entity Framework Core,Asp.net Core 2.0,在集成到现有ASP.NETCore2.0应用程序中后,我遇到了一个问题,即基于ASP.NETCore2.0的新创建的身份验证项目项 经过一些调整,然后成功构建,在浏览器中我得到了下一个错误: 处理请求时发生未处理的异常。 InvalidOperationException:尝试激活时无法解析类型为“SpaServices.ApplicationDbContext”的服务 身份验证必须使用尚未搭建的单独数据库 我的Startup.cs如下所示: public Startup(IHostingEnv
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
.AddEnvironmentVariables();
Configuration = builder.Build();
}
public IConfigurationRoot Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddDbContext<SchoolContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
services.AddMvc()
.AddRazorPagesOptions(options =>
{
options.Conventions.AuthorizeFolder("/Account/Manage");
options.Conventions.AuthorizePage("/Account/Logout");
});
// Register no-op EmailSender used by account confirmation and password reset during development
// For more information on how to enable account confirmation and password reset please visit https://go.microsoft.com/fwlink/?LinkID=532713
services.AddSingleton<IEmailSender, EmailSender>();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
#region webpack-middleware-registration
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseWebpackDevMiddleware();
}
else
{
app.UseExceptionHandler("/Home/Error");
}
// Call UseWebpackDevMiddleware before UseStaticFiles
app.UseStaticFiles();
#endregion
#region mvc-routing-table
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
routes.MapSpaFallbackRoute(
name: "spa-fallback",
defaults: new { controller = "Home", action = "Index" });
});
#endregion
}
如果不成功,有什么想法吗?您需要在ConfigureServices()方法中包括以下内容:
services.AddDbContext(选项=>
options.UseSqlServer(Configuration.GetConnectionString(“[您的连接字符串标识符]”));
目前,您只添加了“SchoolContext”DbContext,您需要在ConfigureServices()方法中包括以下内容:
services.AddDbContext(选项=>
options.UseSqlServer(Configuration.GetConnectionString(“[您的连接字符串标识符]”));
目前,您只添加了一个“SchoolContext”DbContext有时会发生愚蠢的错误,这就是其中之一。是的,您是对的,我添加了它,然后按照上面的方法重新更新构造函数,以显式地期望容器知道如何解析的类型,然后它就工作了。有时会发生愚蠢的错误,这是一个。是的,你是对的,我添加了它,然后像上面一样重新更新了构造函数,以显式地期望容器知道如何解析类型,然后它工作了。
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
}
}
public ApplicationDbContext(DbContextOptions options)
: base(options)
{
}
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("[Your Connection String Identifier]")));