How do I use user-secrets for dbcontext with dotnet ef scaffold tool?

Viewed 136

In my appsettings.json I store the connection string:

"ConnectionStrings": {
    "MyDb": "Server=localhost;Database=MYDB;User Id=sa"
}

I store the password for this connection as a user-secret as per the documentation:

dotnet user-secrets set "Password:Database:MyDb" "password-here"

In code, I setup the connection using SqlConnectionStringBuilder:

var conStrBuilder = new SqlConnectionStringBuilder(builder.Configuration.GetConnectionString("MyDb"));
conStrBuilder.Password = builder.Configuration["Password:Database:MyDb"];
builder.Services.AddSqlServer<MyDbContext>(conStrBuilder.ConnectionString);

Before I moved the password to a user-secret, I was just adding it to the connection string above (i.e. ;Password=password-here). That meant I could then use the following with the dotnet CLI:

dotnet ef dbcontext scaffold Name=ConnectionStrings:MyDb Microsoft.EntityFrameworkCore.SqlServer -o Models

Now of course, this returns Login failed for use 'sa'..

Is there a way to separately pass the password on the command line, so I don't have to put the password in source control or have it plain on the command line?

0 Answers
Related