[英]ConnectionString NULL
我收到此錯誤:
處理請求時發生未處理的異常。 ArgumentNullException:值不能為空。 參數名稱: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": "*"
}
ConfigureServices
之前調用Configure
中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
覆蓋,因為它沒有配置基本路徑,因此很可能導致找不到設置文件。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.