I am trying to seperate my Data Acccess from my WebAPI.
So I made a simple .NET 6 class library (dotnet new classlib -f net6.0 )
for my entity framework core 6
models and context.
I ran dotnet user-secrets init
in the WebAPI project and then copied the <UserSecretsId>
from the .csproj
file to the .csproj
file of the DAL project.
// (school.WebAPI.csproj) added reference in School.WebAPI to School.DAL
<ProjectReference Include="..\School.DAL\School.DAL.csproj" />
Now I am trying to scaffold the database.
dotnet ef dbcontext scaffold name=ConnectionStrings:SchoolDev Microsoft.EntityFrameworkCore.SqlServer -o Models --context-dir Data --schema dbo -f
Which generates the following error:
System.InvalidOperationException: A named connection string was used, but the name 'ConnectionStrings:SchoolDev' was not found in the application's configuration.
When I run the same command in the WebAPI
project it works perfectly.
I read something about var builder = WebApplication.CreateBuilder(args);
being used by the CLI to read the secrets.json
. But I got confused because I don't feel like WebApplication.CreateBuilder(args)
belongs in a DAL
project.
How do you set up access to user-secrets from cli in a class library?
CodePudding user response:
You can run the EF command in the solution root with -s path/to/API_project -p path/to/SQL_project
.
This way it uses the startup project (-s) to figure out configuration and the project (-p) to locate EF context.
Documentation: https://docs.microsoft.com/en-us/ef/core/cli/dotnet#target-project-and-startup-project