Using Scaffold-DbContext with user secrets

.net-core entity-framework-core

Question

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

1
1
11/12/2019 10:05:48 AM

Accepted Answer

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
1
11/12/2019 10:05:48 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow