No database provider has been configured for this DbContext in ASP.NET Core 2.1

asp.net-core-2.1 asp.net-mvc c# entity-framework-6

Question

I have the following error:

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 made all effort, yet it was still correct. However, I am unable to specify the starting connection string.

My ApplicationDbContext is:

public class ApplicationDbContext : DbContext
{
    public ApplicationDbContext(){}
    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options): base(options){}

    public DbSet<Course> Courses { get; set; }
    public DbSet<CourseType> CourseTypes { get; set; }
    public DbSet<CourseState> CourseStates { get; set; }
    public DbSet<Topic> Topics { get; set; }
    public DbSet<Heding> Hedings { get; set; }
}

My start-up is

public void ConfigureServices(IServiceCollection services)
{
     services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
    services.AddDbContext<ApplicationDbContext>(options =>
            options.UseSqlServer(Configuration.GetConnectionString("DbLearning"))));
}

My appSetting.json is :

  "ConnectionStrings": {
    "DbLearning": "Server=(localdb)\\mssqllocaldb;Database=DbLearning;Trusted_Connection=True;"
  }

The issue is not only in start-up but throughoutonConfiguring not an issue

1
1
11/3/2018 2:13:42 PM

Accepted Answer

public void ConfigureServices(IServiceCollection services)
{
    services.AddDbContext<ApplicationDbContext>(options =>
        options.UseSqlServer(Configuration.GetConnectionString("DbLearning")));
}
0
12/2/2019 1:14:47 PM

Popular Answer

ZZZ_tmp


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow