Wednesday, July 13, 2022

Nuget PMC error: 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.

 This error means you have created the DbContext but not configured/added  it to the project using either DI in startup.cs or by using DbContext.OnConfiguring method. 


Add DbContext in starup.cs or configure it by overriding the virtual  DbContext.OnConfiguring  method.

============================================================

  1. protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
  2. {
  3. optionsBuilder.UseSqlServer("server=.;database=myDb;trusted_connection=true;");
  4. }

NOTE to use connectionString from appsetting.json. follow these steps:
1. In appsettings.json add ConnectionStrings section below allowedhosts and add a name-value pair for connection string.
2. Inject IConfiguration in DbContext constructor. You will need to add "using Microsoft.Extensions.Configuration" statement for this.
3. You can now get the conn string by using configuration.GetConnectionString("name");
    public class EmployeeContext : DbContext
    {
        
        private readonly IConfiguration configuration;
        public DbSet<Employee> Employees { get; set; }

        public EmployeeContext(IConfiguration configuration)
        {
            this.configuration = configuration;
        }
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            string str = configuration.GetConnectionString("conn");
            optionsBuilder.UseSqlServer(str);
        }
    }

===================================================================================


  1. public void ConfigureServices(IServiceCollection services)
  2. {
  3. services.AddDbContext<MyDbContext>(options => {
  4. options.UseSqlServer("server=.;database=myDb;trusted_connection=true;"));
  5. });
  6. }

OR
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllers();
            services.AddDbContext<EmployeeContext>(options => {
            options.UseSqlServer(Configuration.GetConnectionString("conn")));
        }



NOTE that once you configure DbContext in ConfigureServices, you must wire it with DbContext by using a constructor that takes DbContextOptions as parameter and passes it to base contructor.


public class EmployeeContext : DbContext { public EmployeeContext(DbContextOptions<EmployeeContext> options) : base(options) { } DbSet<Employee> Employees { get; set; } }
  1. ===================================================================================

No comments:

Post a Comment

How to check local and global angular versions

 Use the command ng version (or ng v ) to find the version of Angular CLI in the current folder. Run it outside of the Angular project, to f...