0

我在現有的ASP.Net核心2.0應用程序中集成了基於ASP.Net Core 2.0的新創建的身份驗證項目後也遇到了問題。無法在嘗試激活時解析「MyApp.ApplicationDbContext」類型的服務

經過一番調整,那麼成功的構建,在browsser我得到了一個錯誤:

An unhandled exception occurred while processing the request.

InvalidOperationException: Unable to resolve service for type 'SpaServices.ApplicationDbContext' while attempting to activate

認證必須使用一個單獨的數據庫,該數據庫還沒有腳手架

我Startup.cs是如下:

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 
} 

我的構造函數如下:

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) 
    { 
    } 

沒有成功,有什麼想法?

回答

1

您需要在您的ConfigureServices方法如下():

services.AddDbContext<ApplicationDbContext>(options => 
    options.UseSqlServer(Configuration.GetConnectionString("[Your Connection String Identifier]"))); 

目前,您只增加了一個「SchoolContext」的DbContext

+0

有時碰巧一個讓愚蠢的錯誤,這是one.Yes你是對的,我添加它,然後重新更新構造函數如上,明確期望容器知道如何解決,然後它的工作。 –

相關問題