【发布时间】:2020-08-08 16:34:04
【问题描述】:
我正在尝试与托管在 Azure 上的数据库建立连接,但我在代码中遇到了一些问题,当我运行应用程序时,它会导致参数中出现错误:connectionString。
STARTUP.CS
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<Contexto>
(
options => options.UseSqlServer("Data Source=Exemple.database.windows.net;Initial Catalog=NameDataDB;Persist Security Info=True;User ID=UserDB;Password=***********")
);
services.AddDbContext<IdentityContexto>
(
options => options.UseSqlServer("Data Source=exemple.database.windows.net;Initial Catalog=NameUserDB;Persist Security Info=True;User ID=userdb;Password=***********")
);
CONTEXTO.CS
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer(configuration.GetConnectionString("NameDB"));
base.OnConfiguring(optionsBuilder);
}
}
当我运行应用程序时出现以下错误:
ArgumentNullException: Value cannot be null. (Parameter 'connectionString')
【问题讨论】:
-
configuration.GetConnectionString("NameDB")) - 这是问题所在,我想是因为使用此代码,您说 ASP.NET Core 查看 appsettings.json 和其他包含的 json 文件并找到 'NameDB ' 参数。
-
this configuration.GetConnectionString("NameDB") 为空,显示你的 appsettings.json,你应该有一个像这样的部分 "ConnectionStrings": { "NameDB": "
" }, -
如果解决了您的问题,请标记答案。
标签: asp.net asp.net-identity asp.net-core-webapi webapi