Search code examples
.net-coreentity-framework-core

Using Scaffold-DbContext with user secrets


I'm trying to reverse engineer a db context using EF Core 3.0 as suggested in this document. It suggests that you can put your connection string into a user secret, and then run:

dotnet ef dbcontext scaffold Name=my_key Microsoft.EntityFrameworkCore.SqlServer

I've tried initialising the user secrets as follows:

dotnet user-secrets init
dotnet user-secrets set ConnectionStrings.my_key "Data Source=........... etc."

and then running the above command, I also also tried this (as I was using the PMC tools)

Scaffold-DbContext Name=my_key Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models

but both give me the same error:

A named connection string was used, but the name 'my_key' 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


Solution

  • The documentation is wrong. You need to include the "ConnectionStrings" prefix in the command as follows:

    Scaffold-DbContext Name=ConnectionStrings.my_key Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models