-2
こんにちは、私はこのエラーを取得しています:と、InvalidOperationExceptionエラー
InvalidOperationException: Unable to resolve service for type 'RegistrationMVC.Model.OurDbContext' while attempting to activate 'RegistrationMVC.Controllers.HomeController'.
私は依存関係で何かすることができ、それを引き起こす可能性がわかりませんか?それができるタイプなので
startup.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using WebApplicationCore.NetCore.DataAccess;
using WebApplicationCore.NetCore.BusinessLogic;
namespace WebApplicationCore.NetCore
public class Startup
{
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.AddMvc();
services.AddSingleton<IConfigurationRoot>(sp => { return this.Configuration; });
services.AddScoped<IContactDataAccess, ContactDataAccess>();
services.AddScoped<IContactBusinessLogic, ContactBusinessLogic>();
}
// 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();
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseBrowserLink();
}
else
{
app.UseExceptionHandler("/Home/Error");
}
app.UseStaticFiles();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}
}
HomeControllerのコンストラクタが必要とするどこにでも、OurDbContextをどこにも登録しません。また、[ask]を読んで、あなたの質問のコード_が[mcve]であることを確認する必要があります。 – CodeCaster