Command "dotnet ef dbcontext scaffold Name=ConnectionString" with secrets.json fails with error "not found in the application's configuration"

Viewed 35

Running dotnet ef dbcontext scaffold name=con Microsoft.EntityFrameworkCore.SqlServer fails with ...the name 'con' was not found in the application's configuration. .... What is missing from the following sequence of steps?

Not a duplication!
Steps:

In an empty folder c:\my\temp\proj I ran the following commands:

dotnet new classlib -f net6.0
dotnet user-secrets init
dotnet user-secrets set con "my connection string"

Then examined proj.csproj and noticed that this entry was added:

<UserSecretsId>bce3b7d0-c6f7-4bd0-b808-841dbfd10bea</UserSecretsId>

And the new secrets.json which was created under:

%APPDATA%\Microsoft\UserSecrets\bce3b7d0-c6f7-4bd0-b808-841dbfd10bea\

Containing:

{
  "con": "my connection string"
}

And then ran:

dotnet add package Microsoft.EntityFrameworkCore.Design
dotnet add package Microsoft.EntityFrameworkCore.SqlServer
dotnet ef dbcontext scaffold name=con Microsoft.EntityFrameworkCore.SqlServer

Which eventually gave this error: A named connection string was used, but the name 'con' was not found in the application's configuration. Note that named connection strings are only supported when using 'IConfiguration' and a service provider, such as in a typical ASP.NET Core application. See https://go.microsoft.com/fwlink/?linkid=850912 for more information.

Even if I create an appsettings.json file containing e.g. { "con": "N/A" } I'm getting the same result.

0 Answers
Related