-1

I'm getting this error:

An unhandled exception occurred while processing the request. ArgumentNullException: Value cannot be null. Parameter name: connectionString

My DbContext:

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

    public DbSet<AcumuladoStock> AcumuladoStockDB { get; set; }
    public DbSet<CabeceraPedidoCliente> CabeceraPedidoClienteDB { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
    }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        base.OnConfiguring(optionsBuilder);
        var builder = new ConfigurationBuilder()
            .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
        IConfigurationRoot config = builder.Build();

        optionsBuilder.UseSqlServer(config.GetConnectionString("DefaultConnection"));
    }
}

public class AcumuladoStock
{
        [Key]
        public int CodigoEmpresa { get; set; }

        public string Ejercicio { get; set; }
        public string CodigoArticulo { get; set; }
        public string Periodo { get; set; }
        public string Partida { get; set; }
        public string UnidadSaldo { get; set; }
}

My startup:

public void ConfigureServices(IServiceCollection services)
{
    // services.AddTransient<GetAcumuladoController>();
    services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
    services.AddDbContext<ApplicationDBContext>(options => 
        options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"))
    );
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    var builder = new ConfigurationBuilder()
       .SetBasePath(env.ContentRootPath)
       .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
       .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true);

    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }
    else
    {
        app.UseHsts();
    }

    app.UseHttpsRedirection();
    app.UseMvc();
}

Appsettings.json:

{ 
  "ConnectionString": { 
    "DefaultConnection": "Server=XXXXX; Database=XXX; User Id=xx; Password=XXXXX; Pooling=true;" 
  }, 
  "Logging": { 
    "LogLevel": { 
      "Default": "Warning" 
    } 
  }, 
  "AllowedHosts": "*" 
} 
GoldenAge
  • 2,918
  • 5
  • 25
  • 63

1 Answers1

1

ConfigureServices is invoked before Configure in Startup. You appear to be building your configuration after already trying to access it.

Move the build code to be invoked earlier in the flow

private IConfiguration Configuration;

public Startup(IHostingEnvironment env) {
    var builder = new ConfigurationBuilder()
       .SetBasePath(env.ContentRootPath)
       .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
       .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true);

    Configuration = builder.Build(); //<--
}

public void ConfigureServices(IServiceCollection services) {

    services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
    services.AddDbContext<ApplicationDBContext>(options => 
        options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"))
    );
}

public void Configure(IApplicationBuilder app, IHostingEnvironment env) {

    if (env.IsDevelopment()) {
        app.UseDeveloperExceptionPage();
    } else {
        app.UseHsts();
    }

    app.UseHttpsRedirection();
    app.UseMvc();
}

You should remove the OnConfiguring override from the DbContext since it does not have a base path configured so most likely it causing the settings file to not be found.

Nkosi
  • 235,767
  • 35
  • 427
  • 472