I'm getting an error when running my .Net Core application. I get to accessing the DbContext and this pops up
System.InvalidOperationException: 'No database provider has been configured for this DbContext. A provider can be configured by overriding the DbContext.OnConfiguring method or by using AddDbContext on the application service provider. If AddDbContext is used, then also ensure that your DbContext type accepts a DbContextOptions object in its constructor and passes it to the base constructor for DbContext.'
I've tried fixing it but it still comes up.
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);
}
}
The error shows up on this line in Towers()
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>();
}
Any help would be nice. I'm sure I'm doing something wrong but I believe I've done all the things that the error suggests.
Thanks.