我在 Asp.NET Core 上使用 MVC,实际上,Startup 没有找到我的 ConnectionString 写入 appsettings.json。我已经尝试了 3 种不同的方式(评论中有 2 种)。不要关注“XXX”。
让我们看看: Startup.cs
public void ConfigureServices(IServiceCollection services)
{
[...]
services.AddDbContext<ApplicationDbContext>(options =>
options.UseOracle(Configuration.GetConnectionString("Web")));
//options.UseOracle(Configuration.GetSection("ConnectionStrings")["Web"]));
//options.UseOracle(Configuration.GetSection("Web")["ConnectionString"]));
[...]
}
appsettings.json:
[...]
"ConnectionStrings": {
"Test1": {
"ConnectionString": "Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=XX.XX.X.XXX)(PORT=XXXX))(CONNECT_DATA=(SERVICE_NAME=XXX)));User Id=XXXXX;Password=XXXXX;",
"ProviderName": "Oracle.ManagedDataAccess.Client"
},
"Web": {
"ConnectionString": "Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=XX.XX.X.XXX)(PORT=XXXX))(CONNECT_DATA=(SERVICE_NAME=XXX)));User Id=XXXXX;Password=XXXXX;",
"ProviderName": "Oracle.ManagedDataAccess.Client"
},
"Test2": {
"ConnectionString": "Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=XX.XX.X.XXX)(PORT=XXXX))(CONNECT_DATA=(SERVICE_NAME=XXX)));User Id=XXXXX;Password=XXXXX;",
"ProviderName": "Oracle.ManagedDataAccess.Client"
}
}
[...]
&当我构建项目以在浏览器中运行时,我总是遇到这个错误:
System.ArgumentNullException : '值不能为空。'
你有什么主意吗 ?