I have two projects, one is the Web and the Other a Class Library. I need to put Entity Framework in the Class Library Project. However, I am unable to read the Connection String when scaffolding the DB.
- Initialize Secret:
dotnet user-secrets init - Set item to store:
dotnet user-secrets set "ConnectionStrings:MyConnString" "string" - Scaffold DB:
dotnet ef dbcontext scaffold Name=ConnectionStrings:MyConnString
This obviously will not work because the static configuration method is in the web project and not the Class Library project.
Error:
A named connection string was used, but the name 'ConnectionStrings:APIConnection' was not found in the application's configuration.
The only way I can make this work is hardcoding the connection string.
Is there anyway other way to make this work?
CodePudding user response:
It works for me in ASP.NET Core 6 and EfCore v6.0.1.
Here the steps I've done:
- Create ASP.NET Core Web API and class lib projects
-- > WebApp.Api\
|
> WebApp.Data\
|
> WebApp.sln
- Set connection string secret for
WebApp.Api. Ensure thatUserSecretsIdis added to csprojPropertyGroup:
> dotnet user-secrets init
> dotnet user-secrets set "ConnectionStrings:MyConnString" "<conn-string>"
- Install
Microsoft.EntityFrameworkCoreandMicrosoft.EntityFrameworkCore.SqlServerintoWebApp.Data. Add project reference fromWebApp.ApitoWebApp.Data. - Install
Microsoft.EntityFrameworkCore.Designnuget forWebApp.Apiproject. - In cmd navigate to
WebApp.Apiproject folder and run the following command:
> dotnet ef dbcontext scaffold Name=ConnectionStrings:MyConnString "Microsoft.EntityFrameworkCore.SqlServer" -p ..\WepApp.Data\
In that case for the scaffold command startup project is WebApp.Api and target project is WebApp.Data
