Hi @Merlyn Coleman,
The error message is caused by the connection string is not in appsettings.json
/ appsettings.Development.json
(it cannot find the connection string If your environment is not Development when you store it in appsettings.Development.json
) .
Be sure your connection string stores in appsettings.json
. And the root key name must be ConnectionStrings, for example:
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"ConnectionStrings": {
"DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=YourDatabaseName;Trusted_Connection=True;MultipleActiveResultSets=true"
}
}
Also be sure the key name is spelled correctly, you can set the breakpoint to check if the connectionString
variable contains value:
var connectionString = builder.Configuration.GetConnectionString("DefaultConnection") ?? throw new InvalidOperationException("Connection string 'DefaultConnection' not found.");
builder.Services.AddDbContext<YourDbContext>(options =>
options.UseSqlServer(connectionString));
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,
Rena