Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

dotnet-cli

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!
  • .NET 6: how to get user-secrets working in a class library? is dealing with copying the <UserSecretsId> tag from one project to another, and moving from/to solution root dir. This question is much simpler:
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.

like image 524
Tar Avatar asked Nov 28 '25 17:11

Tar


1 Answers

I had the same issue, then I noticed the following message: Build failed. Use dotnet build to see the errors.

the result of this command was:

error MSB3021: Unable to copy file "obj\Debug\net6.0\Your_App_Name.dll" to "bin\Debug\net6.0\Your_App_Name.dll". The process cannot access the file 'C:\User
s\...\Your_App_Name.dll' because it is being used by another process.

I realize that I was running debug.. so I just stoped debug and ran command: dotnet ef dbcontext scaffold Name=ConnectionStrings:your_connection_name Microsoft.EntityFrameworkCore.SqlServer --output-dir Models -f

Maybe this could be the same issue you had 🤷‍♂️

reference: https://learn.microsoft.com/pt-br/ef/core/managing-schemas/scaffolding/?tabs=dotnet-core-cli

like image 80
BMesc Avatar answered Dec 02 '25 03:12

BMesc



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!