0

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.

Joe Higley
  • 1,762
  • 3
  • 20
  • 33

1 Answers1

0

Your current Startup.cs file has configured the DB provider for one DB (ApplicationDBContext) but not the one you are using (PartsDbContext).

So, add these lines:

services.AddDbContext<PartsDbContext>(options =>  
    options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"));
)

More information: https://learn.microsoft.com/en-us/ef/core/miscellaneous/configuring-dbcontext#using-dbcontext-with-dependency-injection

Maria Ines Parnisari
  • 16,584
  • 9
  • 85
  • 130
  • I thought that this was suppose to configure the DB provider to be entity-framework: services.AddEntityFramework() .AddDbContext(); – Joe Higley May 03 '17 at 04:45
  • @JoeHigley, Entity Framework is an ORM, not the db provider. The DB provider is SQL, mySql, Oracle, etc. – Maria Ines Parnisari May 03 '17 at 12:50
  • This has fixed the posted issue. However I now have a problem where it doesn't seem that Entity Framework is creating any Tables. I get an error on the same line as before but with this message instead: System.Data.SqlClient.SqlException: 'Invalid object name 'Towers'.' – Joe Higley May 03 '17 at 17:44
  • @JoeHigley I suggest you create a new question for that :) – Maria Ines Parnisari May 04 '17 at 01:39
  • I have done so here [link](http://stackoverflow.com/questions/43767933/entity-framework-system-data-sqlclient-sqlexception-invalid-object-name). Feel free to answer it there if you know what's up. – Joe Higley May 04 '17 at 05:28