Repository navigation
Scaffold-DBContext fails when Database connection string is moved to secrets.json in efcore 9.0.1 and 9.0.2 #35641
Description
Activity
- changed the title
[-]Scaffold-DBContext fails when Database connection is moved to UserSecrets.json 9.0.1 and 9.0.2[/-][+]Scaffold-DBContext fails when Database connection string is moved to secrets.json in efcore 9.0.1 and 9.0.2[/+]on Feb 16, 2025 Microsoft.EntityrameworkCore.Tools from 9.0.0 to 9.0.1 or 9.0.2 give to me same problem.
Microsoft.EntityrameworkCore.Tools 9.0.0 --> Scaffold okPM> Scaffold-DbContext Name=ConnectionStrings:AzureConnection Microsoft.EntityFrameWorkCore.SqlServer -outputdir Models -context AssetCareDbContext -contextdir Repository -DataAnnotations -Force
Build started...
Build succeeded.
PM>
Microsoft.EntityrameworkCore.Tools 9.0.1 --> Scaffold failsPM> Scaffold-DbContext Name=ConnectionStrings:AzureConnection Microsoft.EntityFrameWorkCore.SqlServer -outputdir Models -context AssetCareDbContext -contextdir Repository -DataAnnotations -Force
Build started...
Build succeeded.
An error occurred while accessing the Microsoft.Extensions.Hosting services. Continuing without the application service provider. Error: Value cannot be null. (Parameter 'format')
System.InvalidOperationException: A named connection string was used, but the name 'ConnectionStrings:AzureConnection' 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. See https://go.microsoft.com/fwlink/?linkid=850912 for more information.
at Microsoft.EntityFrameworkCore.Storage.Internal.NamedConnectionStringResolverBase.ResolveConnectionString(String connectionString)
at Microsoft.EntityFrameworkCore.Scaffolding.Internal.ReverseEngineerScaffolder.ScaffoldModel(String connectionString, DatabaseModelFactoryOptions databaseOptions, ModelReverseEngineerOptions modelOptions, ModelCodeGenerationOptions codeOptions)
at Microsoft.EntityFrameworkCore.Design.Internal.DatabaseOperations.ScaffoldContext(String provider, String connectionString, String outputDir, String outputContextDir, String dbContextClassName, IEnumerable1 schemas, IEnumerable1 tables, String modelNamespace, String contextNamespace, Boolean useDataAnnotations, Boolean overwriteFiles, Boolean useDatabaseNames, Boolean suppressOnConfiguring, Boolean noPluralize)
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.ScaffoldContextImpl(String provider, String connectionString, String outputDir, String outputDbContextDir, String dbContextClassName, IEnumerable1 schemaFilters, IEnumerable1 tableFilters, String modelNamespace, String contextNamespace, Boolean useDataAnnotations, Boolean overwriteFiles, Boolean useDatabaseNames, Boolean suppressOnConfiguring, Boolean noPluralize)
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.ScaffoldContext.<>c__DisplayClass0_0.<.ctor>b__0()
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.<>c__DisplayClass3_0`1.b__0()
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
A named connection string was used, but the name 'ConnectionStrings:AzureConnection' 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. See https://go.microsoft.com/fwlink/?linkid=850912 for more information.
PM>Could you add
-Verboseto the command and share the output from 9.0.0 and 9.0.1?
Also, please share the code used to add Configuration?9.0.0 with -Verbose
PM> Scaffold-DbContext -Connection Name=ConnectionStrings:DatabaseConnection -Provider Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models -Context ApplicationDbContext -Force -ContextDir ./Data -NoOnConfiguring -Verbose Using project 'Template_MVC.Web'. Using startup project 'Template_MVC.Web'. Build started... Build succeeded. C:\Program Files\dotnet\dotnet.exe exec --depsfile C:\GITProjects\template-mvc\Template_MVC.Web\bin\Debug\net8.0\Template_MVC.Web.deps.json --additionalprobingpath C:\Users\bhubl\.nuget\packages --additionalprobingpath "c:\Program Files (x86)\Microsoft Visual Studio\Shared\NuGetPackages" --additionalprobingpath "C:\Program Files\dotnet\sdk\NuGetFallbackFolder" --runtimeconfig C:\GITProjects\template-mvc\Template_MVC.Web\bin\Debug\net8.0\Template_MVC.Web.runtimeconfig.json C:\Users\bhubl\.nuget\packages\microsoft.entityframeworkcore.tools\9.0.0\tools\netcoreapp2.0\any\ef.dll dbcontext scaffold Name=ConnectionStrings:DatabaseConnection Microsoft.EntityFrameworkCore.SqlServer --json --output-dir Models --context-dir ./Data --context ApplicationDbContext --force --no-onconfiguring --verbose --no-color --prefix-output --assembly C:\GITProjects\template-mvc\Template_MVC.Web\bin\Debug\net8.0\Template_MVC.Web.dll --project C:\GITProjects\template-mvc\Template_MVC.Web\Template_MVC.Web.csproj --startup-assembly C:\GITProjects\template-mvc\Template_MVC.Web\bin\Debug\net8.0\Template_MVC.Web.dll --startup-project C:\GITProjects\template-mvc\Template_MVC.Web\Template_MVC.Web.csproj --project-dir C:\GITProjects\template-mvc\Template_MVC.Web\ --language C# --configuration Debug --working-dir C:\GITProjects\template-mvc --root-namespace Template_MVC.Web --nullable Using assembly 'Template_MVC.Web'. Using startup assembly 'Template_MVC.Web'. Using application base 'C:\GITProjects\template-mvc\Template_MVC.Web\bin\Debug\net8.0'. Using working directory 'C:\GITProjects\template-mvc\Template_MVC.Web'. Using root namespace 'Template_MVC.Web'. Using project directory 'C:\GITProjects\template-mvc\Template_MVC.Web\'. Remaining arguments: . Finding design-time services referenced by assembly 'Template_MVC.Web'... Finding design-time services referenced by assembly 'Template_MVC.Web'... No referenced design-time services were found. Finding design-time services for provider 'Microsoft.EntityFrameworkCore.SqlServer'... Using design-time services from provider 'Microsoft.EntityFrameworkCore.SqlServer'. Finding IDesignTimeServices implementations in assembly 'Template_MVC.Web'... No design-time services were found. Finding application service provider in assembly 'Template_MVC.Web'... Finding Microsoft.Extensions.Hosting service provider... Using environment 'Development'. Using application service provider from Microsoft.Extensions.Hosting. Found default schema 'dbo'. Found type alias with name 'sys.sysname' which maps to underlying data type nvarchar(128). Found table with name 'dbo.ApplicationEndpoints'. Found table with name 'dbo.Roles'. Found table with name 'dbo.Users'. Found table with name 'dbo.UsersRoles'. Found column with table: dbo.ApplicationEndpoints, column name: Id, ordinal: 1, data type: sys.int, maximum length: 4, precision: 10, scale: 0, nullable: False, identity: True, default value: (null), computed value: (null), computed value is stored: False. Found column with table: dbo.ApplicationEndpoints, column name: Title, ordinal: 2, data type: sys.nvarchar, maximum length: 100, precision: 0, scale: 0, nullable: False, identity: False, default value: (null), computed value: (null), computed value is stored: False. Found column with table: dbo.ApplicationEndpoints, column name: Description, ordinal: 3, data type: sys.varchar, maximum length: 255, precision: 0, scale: 0, nullable: True, identity: False, default value: (null), computed value: (null), computed value is stored: False. Found column with table: dbo.ApplicationEndpoints, column name: Type, ordinal: 4, data type: sys.varchar, maximum length: 50, precision: 0, scale: 0, nullable: True, identity: False, default value: (null), computed value: (null), computed value is stored: False. Found column with table: dbo.ApplicationEndpoints, column name: URL, ordinal: 5, data type: sys.varchar, maximum length: 500, precision: 0, scale: 0, nullable: True, identity: False, default value: (null), computed value: (null), computed value is stored: False. Found column with table: dbo.ApplicationEndpoints, column name: ClientId, ordinal: 6, data type: sys.varchar, maximum length: 255, precision: 0, scale: 0, nullable: True, identity: False, default value: (null), computed value: (null), computed value is stored: False. Found column with table: dbo.ApplicationEndpoints, column name: ClientSecret, ordinal: 7, data type: sys.varchar, maximum length: 255, precision: 0, scale: 0, nullable: True, identity: False, default value: (null), computed value: (null), computed value is stored: False. Found column with table: dbo.ApplicationEndpoints, column name: Username, ordinal: 8, data type: sys.varchar, maximum length: 255, precision: 0, scale: 0, nullable: True, identity: False, default value: (null), computed value: (null), computed value is stored: False. Found column with table: dbo.ApplicationEndpoints, column name: Password, ordinal: 9, data type: sys.varchar, maximum length: 255, precision: 0, scale: 0, nullable: True, identity: False, default value: (null), computed value: (null), computed value is stored: False. Found column with table: dbo.ApplicationEndpoints, column name: Expiration, ordinal: 10, data type: sys.datetime, maximum length: 8, precision: 23, scale: 3, nullable: True, identity: False, default value: (null), computed value: (null), computed value is stored: False. Found column with table: dbo.ApplicationEndpoints, column name: ObjectId, ordinal: 11, data type: sys.varchar, maximum length: 255, precision: 0, scale: 0, nullable: True, identity: False, default value: (null), computed value: (null), computed value is stored: False. Found column with table: dbo.ApplicationEndpoints, column name: BearerToken, ordinal: 12, data type: sys.nvarchar, maximum length: 4096, precision: 0, scale: 0, nullable: True, identity: False, default value: (null), computed value: (null), computed value is stored: False. Found column with table: dbo.ApplicationEndpoints, column name: BearerTokenExpiration, ordinal: 13, data type: sys.datetime, maximum length: 8, precision: 23, scale: 3, nullable: True, identity: False, default value: (null), computed value: (null), computed value is stored: False. Found column with table: dbo.ApplicationEndpoints, column name: Active, ordinal: 14, data type: sys.bit, maximum length: 1, precision: 1, scale: 0, nullable: False, identity: False, default value: ((1)), computed value: (null), computed value is stored: False. Found column with table: dbo.ApplicationEndpoints, column name: TenantId, ordinal: 15, data type: sys.varchar, maximum length: 255, precision: 0, scale: 0, nullable: True, identity: False, default value: (null), computed value: (null), computed value is stored: False. Found column with table: dbo.Roles, column name: Id, ordinal: 1, data type: sys.int, maximum length: 4, precision: 10, scale: 0, nullable: False, identity: True, default value: (null), computed value: (null), computed value is stored: False. Found column with table: dbo.Roles, column name: Name, ordinal: 2, data type: sys.varchar, maximum length: 50, precision: 0, scale: 0, nullable: False, identity: False, default value: (null), computed value: (null), computed value is stored: False. Found column with table: dbo.Roles, column name: Display, ordinal: 3, data type: sys.varchar, maximum length: 255, precision: 0, scale: 0, nullable: False, identity: False, default value: (null), computed value: (null), computed value is stored: False. Found column with table: dbo.Roles, column name: Description, ordinal: 4, data type: sys.varchar, maximum length: 1024, precision: 0, scale: 0, nullable: True, identity: False, default value: (null), computed value: (null), computed value is stored: False. Found column with table: dbo.Roles, column name: Active, ordinal: 5, data type: sys.bit, maximum length: 1, precision: 1, scale: 0, nullable: False, identity: False, default value: ((1)), computed value: (null), computed value is stored: False. Found column with table: dbo.Users, column name: Id, ordinal: 1, data type: sys.int, maximum length: 4, precision: 10, scale: 0, nullable: False, identity: True, default value: (null), computed value: (null), computed value is stored: False. Found column with table: dbo.Users, column name: Username, ordinal: 2, data type: sys.varchar, maximum length: 255, precision: 0, scale: 0, nullable: False, identity: False, default value: (null), computed value: (null), computed value is stored: False. Found column with table: dbo.Users, column name: FirstName, ordinal: 3, data type: sys.varchar, maximum length: 255, precision: 0, scale: 0, nullable: True, identity: False, default value: (null), computed value: (null), computed value is stored: False. Found column with table: dbo.Users, column name: LastName, ordinal: 4, data type: sys.varchar, maximum length: 255, precision: 0, scale: 0, nullable: True, identity: False, default value: (null), computed value: (null), computed value is stored: False. Found column with table: dbo.Users, column name: Phone, ordinal: 5, data type: sys.varchar, maximum length: 50, precision: 0, scale: 0, nullable: True, identity: False, default value: (null), computed value: (null), computed value is stored: False. Found column with table: dbo.Users, column name: Email, ordinal: 6, data type: sys.varchar, maximum length: 255, precision: 0, scale: 0, nullable: True, identity: False, default value: (null), computed value: (null), computed value is stored: False. Found column with table: dbo.Users, column name: Active, ordinal: 7, data type: sys.bit, maximum length: 1, precision: 1, scale: 0, nullable: False, identity: False, default value: ((1)), computed value: (null), computed value is stored: False. Found column with table: dbo.Users, column name: DarkMode, ordinal: 8, data type: sys.bit, maximum length: 1, precision: 1, scale: 0, nullable: False, identity: False, default value: ((1)), computed value: (null), computed value is stored: False. Found column with table: dbo.UsersRoles, column name: Id, ordinal: 1, data type: sys.int, maximum length: 4, precision: 10, scale: 0, nullable: False, identity: True, default value: (null), computed value: (null), computed value is stored: False. Found column with table: dbo.UsersRoles, column name: UserId, ordinal: 2, data type: sys.int, maximum length: 4, precision: 10, scale: 0, nullable: False, identity: False, default value: (null), computed value: (null), computed value is stored: False. Found column with table: dbo.UsersRoles, column name: RoleId, ordinal: 3, data type: sys.int, maximum length: 4, precision: 10, scale: 0, nullable: False, identity: False, default value: (null), computed value: (null), computed value is stored: False. Found column with table: dbo.UsersRoles, column name: Active, ordinal: 4, data type: sys.bit, maximum length: 1, precision: 1, scale: 0, nullable: False, identity: False, default value: ((1)), computed value: (null), computed value is stored: False. Found primary key on table 'PK_Endpoints' with name 'dbo.ApplicationEndpoints'. Found unique constraint on table 'UQ__Endpoint__Title' with name 'dbo.ApplicationEndpoints'. Found primary key on table 'PK_Roles' with name 'dbo.Roles'. Found primary key on table 'PK_Users' with name 'dbo.Users'. Found primary key on table 'PK_UsersRoles' with name 'dbo.UsersRoles'. Found foreign key on table 'FK_UsersRoles_Roles' with name 'dbo.UsersRoles', principal table 'dbo.Roles', delete action CASCADE. Found foreign key on table 'FK_UsersRoles_Users' with name 'dbo.UsersRoles', principal table 'dbo.Users', delete action CASCADE. PM>9.0.1 with -Verbose
PM> Scaffold-DbContext -Connection Name=ConnectionStrings:DatabaseConnection -Provider Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models -Context ApplicationDbContext -Force -ContextDir ./Data -NoOnConfiguring -Verbose Using project 'Template_MVC.Web'. Using startup project 'Template_MVC.Web'. Build started... Build succeeded. C:\Program Files\dotnet\dotnet.exe exec --depsfile C:\GITProjects\template-mvc\Template_MVC.Web\bin\Debug\net8.0\Template_MVC.Web.deps.json --additionalprobingpath C:\Users\bhubl\.nuget\packages --additionalprobingpath "c:\Program Files (x86)\Microsoft Visual Studio\Shared\NuGetPackages" --additionalprobingpath "C:\Program Files\dotnet\sdk\NuGetFallbackFolder" --runtimeconfig C:\GITProjects\template-mvc\Template_MVC.Web\bin\Debug\net8.0\Template_MVC.Web.runtimeconfig.json C:\Users\bhubl\.nuget\packages\microsoft.entityframeworkcore.tools\9.0.1\tools\netcoreapp2.0\any\ef.dll dbcontext scaffold Name=ConnectionStrings:DatabaseConnection Microsoft.EntityFrameworkCore.SqlServer --json --output-dir Models --context-dir ./Data --context ApplicationDbContext --force --no-onconfiguring --verbose --no-color --prefix-output --assembly C:\GITProjects\template-mvc\Template_MVC.Web\bin\Debug\net8.0\Template_MVC.Web.dll --project C:\GITProjects\template-mvc\Template_MVC.Web\Template_MVC.Web.csproj --startup-assembly C:\GITProjects\template-mvc\Template_MVC.Web\bin\Debug\net8.0\Template_MVC.Web.dll --startup-project C:\GITProjects\template-mvc\Template_MVC.Web\Template_MVC.Web.csproj --project-dir C:\GITProjects\template-mvc\Template_MVC.Web\ --language C# --configuration Debug --working-dir C:\GITProjects\template-mvc --root-namespace Template_MVC.Web --nullable Using assembly 'Template_MVC.Web'. Using startup assembly 'Template_MVC.Web'. Using application base 'C:\GITProjects\template-mvc\Template_MVC.Web\bin\Debug\net8.0'. Using working directory 'C:\GITProjects\template-mvc\Template_MVC.Web'. Using root namespace 'Template_MVC.Web'. Using project directory 'C:\GITProjects\template-mvc\Template_MVC.Web\'. Remaining arguments: . Finding design-time services referenced by assembly 'Template_MVC.Web'... Finding design-time services referenced by assembly 'Template_MVC.Web'... No referenced design-time services were found. Finding design-time services for provider 'Microsoft.EntityFrameworkCore.SqlServer'... Using design-time services from provider 'Microsoft.EntityFrameworkCore.SqlServer'. Finding IDesignTimeServices implementations in assembly 'Template_MVC.Web'... No design-time services were found. Finding application service provider in assembly 'Template_MVC.Web'... Finding Microsoft.Extensions.Hosting service provider... Using application service provider from Microsoft.Extensions.Hosting. System.InvalidOperationException: A named connection string was used, but the name 'ConnectionStrings:DatabaseConnection' 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. See https://go.microsoft.com/fwlink/?linkid=850912 for more information. at Microsoft.EntityFrameworkCore.Storage.Internal.NamedConnectionStringResolverBase.ResolveConnectionString(String connectionString) at Microsoft.EntityFrameworkCore.Scaffolding.Internal.ReverseEngineerScaffolder.ScaffoldModel(String connectionString, DatabaseModelFactoryOptions databaseOptions, ModelReverseEngineerOptions modelOptions, ModelCodeGenerationOptions codeOptions) at Microsoft.EntityFrameworkCore.Design.Internal.DatabaseOperations.ScaffoldContext(String provider, String connectionString, String outputDir, String outputContextDir, String dbContextClassName, IEnumerable`1 schemas, IEnumerable`1 tables, String modelNamespace, String contextNamespace, Boolean useDataAnnotations, Boolean overwriteFiles, Boolean useDatabaseNames, Boolean suppressOnConfiguring, Boolean noPluralize) at Microsoft.EntityFrameworkCore.Design.OperationExecutor.ScaffoldContextImpl(String provider, String connectionString, String outputDir, String outputDbContextDir, String dbContextClassName, IEnumerable`1 schemaFilters, IEnumerable`1 tableFilters, String modelNamespace, String contextNamespace, Boolean useDataAnnotations, Boolean overwriteFiles, Boolean useDatabaseNames, Boolean suppressOnConfiguring, Boolean noPluralize) at Microsoft.EntityFrameworkCore.Design.OperationExecutor.ScaffoldContext.<>c__DisplayClass0_0.<.ctor>b__0() at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.<>c__DisplayClass3_0`1.<Execute>b__0() at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action) A named connection string was used, but the name 'ConnectionStrings:DatabaseConnection' 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. See https://go.microsoft.com/fwlink/?linkid=850912 for more information. PM>Scaffold-DbContext Name=ConnectionStrings:AzureConnection Microsoft.EntityFrameWorkCore.SqlServer -outputdir Models -context AssetCareDbContext -contextdir Repository -DataAnnotations -Force
Works with Microsoft.EntityrameworkCore.Tools 9.0.0 and olders and not stop working with 9.0.1 and 9.0.2.
I need for last version to copy my secrets on appsettings.jsonWith Microsoft.EntityFrameworkCore.Tools 9.0.2
PM> Scaffold-DbContext Name=ConnectionStrings:AzureConnection Microsoft.EntityFrameWorkCore.SqlServer -outputdir Models -context AssetCareDbContext -contextdir Repository -DataAnnotations -Force -Verbose
Using project 'AssetCareInfrastructure'.
Using startup project 'AssetCareWebAPI'.
Build started...
Build succeeded.
C:\Program Files\dotnet\dotnet.exe exec --depsfile "C:\APPs\DEVOPS\CODEXASSETCARE\Codex Backend\AssetCareWebAPI\bin\Debug\net9.0\AssetCareWebAPI.deps.json" --additionalprobingpath C:\Users\fernandez.jo.nuget\packages --additionalprobingpath "C:\Program Files (x86)\Microsoft Visual Studio\Shared\NuGetPackages" --runtimeconfig "C:\APPs\DEVOPS\CODEXASSETCARE\Codex Backend\AssetCareWebAPI\bin\Debug\net9.0\AssetCareWebAPI.runtimeconfig.json" C:\Users\fernandez.jo.nuget\packages\microsoft.entityframeworkcore.tools\9.0.2\tools\netcoreapp2.0\any\ef.dll dbcontext scaffold Name=ConnectionStrings:AzureConnection Microsoft.EntityFrameWorkCore.SqlServer --json --output-dir Models --context-dir Repository --context AssetCareDbContext --data-annotations --force --verbose --no-color --prefix-output --assembly "C:\APPs\DEVOPS\CODEXASSETCARE\Codex Backend\AssetCareWebAPI\bin\Debug\net9.0\AssetCareInfrastructure.dll" --project "C:\APPs\DEVOPS\CODEXASSETCARE\Codex Backend\AssetCareInfrastructure\AssetCareInfrastructure.csproj" --startup-assembly "C:\APPs\DEVOPS\CODEXASSETCARE\Codex Backend\AssetCareWebAPI\bin\Debug\net9.0\AssetCareWebAPI.dll" --startup-project "C:\APPs\DEVOPS\CODEXASSETCARE\Codex Backend\AssetCareWebAPI\AssetCareWebAPI.csproj" --project-dir "C:\APPs\DEVOPS\CODEXASSETCARE\Codex Backend\AssetCareInfrastructure\" --language C# --configuration Debug --working-dir "C:\APPs\DEVOPS\CODEXASSETCARE\Codex Backend" --root-namespace AssetCareInfrastructure --nullable
Using assembly 'AssetCareInfrastructure'.
Using startup assembly 'AssetCareWebAPI'.
Using application base 'C:\APPs\DEVOPS\CODEXASSETCARE\Codex Backend\AssetCareWebAPI\bin\Debug\net9.0'.
Using working directory 'C:\APPs\DEVOPS\CODEXASSETCARE\Codex Backend\AssetCareWebAPI'.
Using root namespace 'AssetCareInfrastructure'.
Using project directory 'C:\APPs\DEVOPS\CODEXASSETCARE\Codex Backend\AssetCareInfrastructure'.
Remaining arguments: .
Finding design-time services referenced by assembly 'AssetCareWebAPI'...
Finding design-time services referenced by assembly 'AssetCareInfrastructure'...
No referenced design-time services were found.
Finding design-time services for provider 'Microsoft.EntityFrameWorkCore.SqlServer'...
Using design-time services from provider 'Microsoft.EntityFrameWorkCore.SqlServer'.
Finding IDesignTimeServices implementations in assembly 'AssetCareWebAPI'...
No design-time services were found.
Finding application service provider in assembly 'AssetCareWebAPI'...
Finding Microsoft.Extensions.Hosting service provider...
System.ArgumentNullException: Value cannot be null. (Parameter 'format')
at System.ArgumentNullException.Throw(String paramName)
at System.String.FormatHelper(IFormatProvider provider, String format, ReadOnlySpan1 args) at Program.<Main>$(String[] args) in C:\APPs\DEVOPS\CODEXASSETCARE\Codex Backend\AssetCareWebAPI\Program.cs:line 42 at Program.<Main>(String[] args) at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor) at System.Reflection.MethodBaseInvoker.InvokeDirectByRefWithFewArgs(Object obj, Span1 copyOfArgs, BindingFlags invokeAttr)
--- End of stack trace from previous location ---
at Microsoft.Extensions.Hosting.HostFactoryResolver.HostingListener.CreateHost()
at Microsoft.Extensions.Hosting.HostFactoryResolver.<>c__DisplayClass10_0.b__0(String[] args)
at Microsoft.Extensions.Hosting.HostFactoryResolver.<>c__DisplayClass13_0.b__3(String[] args)
at Microsoft.EntityFrameworkCore.Design.Internal.AppServiceProviderFactory.CreateFromHosting(String[] args)
An error occurred while accessing the Microsoft.Extensions.Hosting services. Continuing without the application service provider. Error: Value cannot be null. (Parameter 'format')
No application service provider was found.
System.InvalidOperationException: A named connection string was used, but the name 'ConnectionStrings:AzureConnection' 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. See https://go.microsoft.com/fwlink/?linkid=850912 for more information.
at Microsoft.EntityFrameworkCore.Storage.Internal.NamedConnectionStringResolverBase.ResolveConnectionString(String connectionString)
at Microsoft.EntityFrameworkCore.Scaffolding.Internal.ReverseEngineerScaffolder.ScaffoldModel(String connectionString, DatabaseModelFactoryOptions databaseOptions, ModelReverseEngineerOptions modelOptions, ModelCodeGenerationOptions codeOptions)
at Microsoft.EntityFrameworkCore.Design.Internal.DatabaseOperations.ScaffoldContext(String provider, String connectionString, String outputDir, String outputContextDir, String dbContextClassName, IEnumerable1 schemas, IEnumerable1 tables, String modelNamespace, String contextNamespace, Boolean useDataAnnotations, Boolean overwriteFiles, Boolean useDatabaseNames, Boolean suppressOnConfiguring, Boolean noPluralize)
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.ScaffoldContextImpl(String provider, String connectionString, String outputDir, String outputDbContextDir, String dbContextClassName, IEnumerable1 schemaFilters, IEnumerable1 tableFilters, String modelNamespace, String contextNamespace, Boolean useDataAnnotations, Boolean overwriteFiles, Boolean useDatabaseNames, Boolean suppressOnConfiguring, Boolean noPluralize)
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.ScaffoldContext.<>c__DisplayClass0_0.<.ctor>b__0()
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.<>c__DisplayClass3_0`1.b__0()
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
A named connection string was used, but the name 'ConnectionStrings:AzureConnection' 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. See https://go.microsoft.com/fwlink/?linkid=850912 for more information.
PM>With Microsoft.EntityFrameworkCore.Tools 9.0.0
PM> Scaffold-DbContext Name=ConnectionStrings:AzureConnection Microsoft.EntityFrameWorkCore.SqlServer -outputdir Models -context AssetCareDbContext -contextdir Repository -DataAnnotations -Force -Verbose
Using project 'AssetCareInfrastructure'.
Using startup project 'AssetCareWebAPI'.
Build started...
Build succeeded.
C:\Program Files\dotnet\dotnet.exe exec --depsfile "C:\APPs\DEVOPS\CODEXASSETCARE\Codex Backend\AssetCareWebAPI\bin\Debug\net9.0\AssetCareWebAPI.deps.json" --additionalprobingpath C:\Users\fernandez.jo.nuget\packages --additionalprobingpath "C:\Program Files (x86)\Microsoft Visual Studio\Shared\NuGetPackages" --runtimeconfig "C:\APPs\DEVOPS\CODEXASSETCARE\Codex Backend\AssetCareWebAPI\bin\Debug\net9.0\AssetCareWebAPI.runtimeconfig.json" C:\Users\fernandez.jo.nuget\packages\microsoft.entityframeworkcore.tools\9.0.2\tools\netcoreapp2.0\any\ef.dll dbcontext scaffold Name=ConnectionStrings:AzureConnection Microsoft.EntityFrameWorkCore.SqlServer --json --output-dir Models --context-dir Repository --context AssetCareDbContext --data-annotations --force --verbose --no-color --prefix-output --assembly "C:\APPs\DEVOPS\CODEXASSETCARE\Codex Backend\AssetCareWebAPI\bin\Debug\net9.0\AssetCareInfrastructure.dll" --project "C:\APPs\DEVOPS\CODEXASSETCARE\Codex Backend\AssetCareInfrastructure\AssetCareInfrastructure.csproj" --startup-assembly "C:\APPs\DEVOPS\CODEXASSETCARE\Codex Backend\AssetCareWebAPI\bin\Debug\net9.0\AssetCareWebAPI.dll" --startup-project "C:\APPs\DEVOPS\CODEXASSETCARE\Codex Backend\AssetCareWebAPI\AssetCareWebAPI.csproj" --project-dir "C:\APPs\DEVOPS\CODEXASSETCARE\Codex Backend\AssetCareInfrastructure\" --language C# --configuration Debug --working-dir "C:\APPs\DEVOPS\CODEXASSETCARE\Codex Backend" --root-namespace AssetCareInfrastructure --nullable
Using assembly 'AssetCareInfrastructure'.
Using startup assembly 'AssetCareWebAPI'.
Using application base 'C:\APPs\DEVOPS\CODEXASSETCARE\Codex Backend\AssetCareWebAPI\bin\Debug\net9.0'.
Using working directory 'C:\APPs\DEVOPS\CODEXASSETCARE\Codex Backend\AssetCareWebAPI'.
Using root namespace 'AssetCareInfrastructure'.
Using project directory 'C:\APPs\DEVOPS\CODEXASSETCARE\Codex Backend\AssetCareInfrastructure'.
Remaining arguments: .
Finding design-time services referenced by assembly 'AssetCareWebAPI'...
Finding design-time services referenced by assembly 'AssetCareInfrastructure'...
No referenced design-time services were found.
Finding design-time services for provider 'Microsoft.EntityFrameWorkCore.SqlServer'...
Using design-time services from provider 'Microsoft.EntityFrameWorkCore.SqlServer'.
Finding IDesignTimeServices implementations in assembly 'AssetCareWebAPI'...
No design-time services were found.
Finding application service provider in assembly 'AssetCareWebAPI'...
Finding Microsoft.Extensions.Hosting service provider...
Using environment 'Development'.
Using application service provider from Microsoft.Extensions.Hosting.
Found default schema 'dbo'.
Found type alias with name 'sys.vector' which maps to underlying data type varbinary(8000).
Found type alias with name 'sys.sysname' which maps to underlying data type nvarchar(128).
Found table with name 'dbo.ChangeLog'.
Found table with name 'dbo.CustomerPortalSession'.
Found table with name 'dbo.DeviceList'.
Found table with name 'dbo.DeviceListAttachments'.
Found table with name 'dbo.DeviceListCVEs'.
Found table with name 'dbo.DeviceListCVEsLog'.
Found table with name 'dbo.DeviceListNetworks'.
Found table with name 'dbo.DeviceProducts'.
.....
I DO NOT SEND ALL CODE - IS VERY LARGEI see. The
9.0.0output containsUsing environment 'Development'., so this regression was introduced in 017e8dc.As a workaround, set
$env:ASPNETCORE_ENVIRONMENT = 'Development'before running the command@AndriySvyryd thank you for the workaround. I can confirm this fixes the issue. Will this be resolved in a future edition?
4 remaining items
- added a commit that references this issue
on Feb 26, 2025 - added 3 commits that reference this issue
on Feb 27, 2025 I can confirm 9.0.3 Still need to execute "$env:ASPNETCORE_ENVIRONMENT = 'Development'" in order to execute scaffold correctly.
Reacted by Michal Přikryl and OWEBCyrus- marked [BUG] Microsoft.EntityFrameworkCore.Design 9.0.1+ does not respect connection strings in usersecrets.json #35774 as a duplicate of this issue
on Mar 12, 2025
Bug description
Scaffold-DBContext works when database connection is setup in appsettings.json in all versions tested. 8.0.8-9.0.2. When moving the database connection to secrets.json, the Scaffold-DBContext command works as expected in versions 8.0.8-9.0.0. I receive a Database Connection was not found in the applications configuration error in versions 9.0.1 and 9.0.2. Rolling the Microsoft.EntityFrameworkCore.SqlServer and Microsoft.EntityrameworkCore.Tools back to version9.0.0 or earlier, Scaffold-DBContext command works using secrets.json.
Your code
Stack traces
Verbose output
EF Core version
9.0.2
Database provider
No response
Target framework
.NET 8.0
Operating system
Windows 11
IDE
Visual Studio 2022 v17.4