.Net Coreアプリケーションを実行しているときにエラーが発生しています。 DbContextにアクセスすると、これがポップアップします。System.InvalidOperationException:DbContextにアクセスするとき
System.InvalidOperationException: 'このDbContextに対してデータベースプロバイダが設定されていません。プロバイダーは、DbContext.OnConfiguringメソッドをオーバーライドするか、アプリケーションサービスプロバイダーでAddDbContextを使用して構成できます。 AddDbContextが使用されている場合は、DbContext型がそのコンストラクタ内のDbContextOptionsオブジェクトを受け入れ、DbContextのベースコンストラクタに渡していることも確認してください。 '
私はそれを修正しようとしましたが、まだそれが起こります。
DbContext.cs
public class PartsDbContext : DbContext
{
public DbSet<Tower> Towers { get; set; }
public DbSet<Motherboard> Motherboards { get; set; }
public PartsDbContext(DbContextOptions<PartsDbContext> options)
: base(options)
{
}
}
Controller.cs
public class AdminController : Controller
{
private readonly PartsDbContext _context;
public AdminController(PartsDbContext context)
{
_context = context;
}
public IActionResult Index()
{
if (User.Identity.Name == null)
{
return RedirectToAction("Register", "Account");
}
return View();
}
public IActionResult Towers()
{
var model = _context.Towers.ToList();
return View(model);
}
public IActionResult Motherboards()
{
var model = _context.Motherboards.ToList();
return View(model);
}
}
エラー(タワーこのライン上に示す)
var model = _context.Towers.ToList();
Startup.cs
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
services.AddEntityFramework()
.AddDbContext<PartsDbContext>();
services.AddMvc();
// Add application services.
services.AddTransient<IEmailSender, AuthMessageSender>();
services.AddTransient<ISmsSender, AuthMessageSender>();
}
すべてのヘルプがいいだろう。私は何か間違っていると確信していますが、私はエラーが示唆するすべてのことをやったと信じています。
ありがとうございました。
は、あなたが実際にあなたの 'App.config'で構成された' DefaultConnection'を持っていますか? –
App.configはありません。私はASP.Net Core 1.1を使用しており、App.configを使用しているとは思わない。 –
ああ、申し訳ありませんが、 'appsettings.json'です。 https://docs.microsoft.com/en-us/ef/core/miscellaneous/connection-strings#aspnet-core –