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": "*"
}