ConnectionString NULL

时间:2019-04-06 19:50:23

标签: c# asp.net-core .net-core argumentnullexception

我收到此错误:

  

处理请求时发生未处理的异常。   ArgumentNullException:值不能为null。   参数名称:connectionString

我的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; }
}

我的创业公司:

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

1 个答案:

答案 0 :(得分:1)

ConfigureServicesConfigure中的Startup之前被调用。在尝试访问配置后,您似乎正在构建配置。

移动要在流程中更早调用的构建代码

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();
}

您应该从DbContext中删除OnConfiguring覆盖,因为它没有配置基本路径,因此很可能导致找不到设置文件。

相关问题