Hi @tjestesjr77
System.InvalidOperationException: The ConnectionString property has not been initialized.
builder.Configuration.GetConnectionString("QA_Context")
This issue relates the above code.
When use the GetConnectionString()
method, it will look for a configuration value whose key is ConnectionStrings:<connection string name>
. But in your appsettings.json
file, the QA_Context
doesn't locate inside the ConnectionStrings
key, so the connection string will be null, and will show the ConnectionString property has not been initialized error.
To solve this issue, you can get the QA_Context
value use: builder.Configuration["ConnectionStringQA:QA_Context"]
builder.Services.AddDbContext<QSCC_QAContext>(options =>
options.UseSqlServer(builder.Configuration["ConnectionStringQA:QA_Context"]));
Or you can change the appsettings.json
file as below:
{
"ConnectionStrings": {
"DefaultConnection": "Server=QSCCWSQL01.QSCC.local;Database=QsccDevAlpha;Trusted_Connection=True;MultipleActiveResultSets=true;TrustServerCertificate=True;Encrypt=True",
"QA_Context": "Data Source=QSCCWSQL01.qscc.local;Initial Catalog=QSCC_QA;Integrated Security=True;TrustServerCertificate=True;MultipleActiveResultSets=true;Encrypt=True"
},
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*",
}
Then you can use the GetConnectionString()
method to get the connection string, in this scenario, use the following code:
builder.Services.AddDbContext<QSCC_QAContext>(options =>
options.UseSqlServer(builder.Configuration.GetConnectionString("QA_Context")));
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
Best regards,
Dillion