Command "dotnet ef dbcontext scaffold Name=ConnectionString" with secrets.json fails with error "not found in the application's configuration" - dotnet-cli

Running dotnet ef dbcontext scaffold name=con Microsoft.EntityFrameworkCore.SqlServer fails with ...the name 'con' was not found in the application's configuration. .... What is missing from the following sequence of steps?
Not a duplication!
.NET 6: how to get user-secrets working in a class library? is dealing with copying the <UserSecretsId> tag from one project to another, and moving from/to solution root dir. This question is much simpler:
Steps:
In an empty folder c:\my\temp\proj I ran the following commands:
dotnet new classlib -f net6.0
dotnet user-secrets init
dotnet user-secrets set con "my connection string"
Then examined proj.csproj and noticed that this entry was added:
<UserSecretsId>bce3b7d0-c6f7-4bd0-b808-841dbfd10bea</UserSecretsId>
And the new secrets.json which was created under:
%APPDATA%\Microsoft\UserSecrets\bce3b7d0-c6f7-4bd0-b808-841dbfd10bea\
Containing:
{
"con": "my connection string"
}
And then ran:
dotnet add package Microsoft.EntityFrameworkCore.Design
dotnet add package Microsoft.EntityFrameworkCore.SqlServer
dotnet ef dbcontext scaffold name=con Microsoft.EntityFrameworkCore.SqlServer
Which eventually gave this error: A named connection string was used, but the name 'con' 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.
Even if I create an appsettings.json file containing e.g. { "con": "N/A" } I'm getting the same result.

I had the same issue, then I noticed the following message: Build failed. Use dotnet build to see the errors.
the result of this command was:
error MSB3021: Unable to copy file "obj\Debug\net6.0\Your_App_Name.dll" to "bin\Debug\net6.0\Your_App_Name.dll". The process cannot access the file 'C:\User
s\...\Your_App_Name.dll' because it is being used by another process.
I realize that I was running debug.. so I just stoped debug and ran command: dotnet ef dbcontext scaffold Name=ConnectionStrings:your_connection_name Microsoft.EntityFrameworkCore.SqlServer --output-dir Models -f
Maybe this could be the same issue you had 🤷‍♂️
reference: https://learn.microsoft.com/pt-br/ef/core/managing-schemas/scaffolding/?tabs=dotnet-core-cli

Related

Unable to run default ABP project

I've followed the instructions and created a new solution using command
abp new Acme.BookStore -u blazor-server --tiered --preview
When I try and run the solution i get an error:
AbpException: Could not find the bundle file '/libs/bootstrap/css/bootstrap.css' for the bundle 'Blazor.LeptonXLiteTheme.Global'!
I've tried running abp install-libs but still doesn't work. Any ideas?

MSB4062 error publishing .NET7 app with publish profile

This is pretty easy to replicate but also code here:
https://github.com/dominicshaw/dotnet-publish-error
create an empty wpf project using .NET7
create a publish profile for ClickOnce
publish via command line (not visual studio)
Publish profile is almost totally standard (view here)
Command line from project directory:
dotnet publish PublishError.csproj -p:PublishProfile=ClickOnceProfile
Subsequent error:
MSBuild version 17.4.0+18d5aef85 for .NET
Determining projects to restore...
All projects are up-to-date for restore.
C:\Program Files\dotnet\sdk\7.0.100\Microsoft.Common.CurrentVersion.targets(4149,5): error MSB4062: The "Microsoft.Build.Tasks.RequiresFr
amework35SP1Assembly" task could not be loaded from the assembly Microsoft.Build.Tasks.Core, Version=15.1.0.0, Culture=neutral, PublicKey
Token=b03f5f7f11d50a3a. Confirm that the <UsingTask> declaration is correct, that the assembly and all its dependencies are available, a
nd that the task contains a public class that implements Microsoft.Build.Framework.ITask. [C:\Users\shawd\source\repos\PublishError\Publi
shError\PublishError.csproj]
I have the latest SDKs installed and VS2022 up to date. This only happens via command line - I am able to publish from Visual Studio (I am setting up devops, so require command line).
This happens regardless of adding nuget package for Microsoft.Build.Tasks.Core. Note however nuget package v15.1.0.0 is not available - I have tried 15.1.548 and the latest instead.
Totally at a loss! Any ideas very welcome.
Thanks vm
During the tests with your project and files, I finally managed to publish it successfully with the command dotnet publish PublishError.csproj -p:PublishProfile=FolderProfile.
Here is the document for dotnet publish, as is referred,
The preceding example uses the FolderProfile.pubxml file that is found
in the <project_folder>/Properties/PublishProfiles folder. If you
specify a path and file extension when setting the PublishProfile
property, they are ignored. MSBuild by default looks in the
Properties/PublishProfiles folder and assumes the pubxml file
extension.
And I also succeeded with dotnet publish with your project.
==============================
update on 11/19
So I suppose that the issue is resulted from your definition for -p:PublishProfile=ClickOnceProfile,it will change the path where this command would search for the publish file
A partial answer as my workaround for anyone struggling with a similar problem:
I have found it impossible to do this with the dotnet cli but I can do it with MSBuild if I specifically target 17.4 (VS2022). By default, my Azure DevOps Server 2020 capability is the 2019 MSBuild (15.X), so I have to be explicit:
"C:\Program Files\Microsoft Visual Studio\2022\Professional\Msbuild\Current\Bin\MSBuild.exe" /restore /Verbosity:m /t:Publish /p:RuntimeIdentifier=win-x64 /p:configuration=Release /p:PublishProfile=ClickOnceProfile
This works - so in my pipeline I have created a batch file which runs this, then I copy the files to my ClickOnce location.
Not ideal, but a working pipeline.
Interesting side note - this dotnet cli command which should be idenitical (and indeed uses the correct 17.4 MSBuild) - does not work (with the same error):
dotnet msbuild -target:Publish -property:RuntimeIdentifiers=win-x64;Configuration=Release;PublishProfile=ClickOnceProfile

Use name connectionStrings in app.config for Scaffold-DbContext EF 6 command

I've a "litle" problem to scaffold an existing database from SQL Server using Entity Framework 6 in my console app...
My connection string in App.config is:
<connectionStrings>
<add name="connString"
connectionString="Server=MyServer; Database=MyDb;User Id=MYUser ; Password=MyPW ; MultipleActiveResultSets=true;"
providerName="System.Data.SqlClient"/>
</connectionStrings>
If I try to scaffold my database with this command:
Scaffold-DbContext -Connection name=connString Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models -Context MyDbContext -force
or
Scaffold-DbContext -Connection name=connectionString Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models -Context MyDbContext -force
or
Scaffold-DbContext "Name=connectionStrings:connString" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models -Context MyDbContext -force
The console always returns an error:
A named connection string was used, but the name '.......' 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.
So I tried to scaffold using plaintext connection string, and it didn't return any errors, but only the suggestion:
To protect potentially sensitive information in your connection string, you should move it out of source code. You can avoid scaffolding the connection string by using the Name= syntax to read it from configuration - see https://go.microsoft.com/fwlink/?linkid=2131148. For more guidance on storing connection strings, see http://go.microsoft.com/fwlink/?LinkId=723263.
So my question is:
What should I do to be able to write only the name of my connection string, rather than the connection string in clear text?
Thanks in advance
In my case (VS2022, .NET 6) I also encountered the same problem n got solution in following way;
Added Connection String in appsettings.json file (Screenshot attached)
Ran the command: PM> Scaffold-DbContext name=StudentDbConnectionString TrustServerCertificate=true;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models
It generated relevant DbContext file (Screenshot attached)
Manually registered DbContext in Builder.Services in program.cs file (Screenshot attached)
Rebuilt application
Run application and all the previously implemented functionalities tested successfully.

Create a WinForm project based on .net core 3

I am trying to figure out what I am missing here, must be something quite obvious but I am unable to see it.
what I am trying to achieve is to have a working solution/project of WinForms app based on .net core 3. it might be too early to look into it as they just announced it but still why not if we can. here are steps I've done so far:
create a blank solution
run the ".net new winforms" command
attempt to add the generated project to the solution.
i'm getting the error: Project File is incomplete. Expected imports are missing.
here is the output of the CLI:
Package Manager Console Host Version 4.9.2.5706
Type 'get-help NuGet' to see all available NuGet commands.
PM> dotnet --list-sdks
2.1.202 [C:\Program Files\dotnet\sdk]
2.1.502 [C:\Program Files\dotnet\sdk]
2.2.105 [C:\Program Files\dotnet\sdk]
3.0.100-preview3-010431 [C:\Program Files\dotnet\sdk]
PM> dotnet new winforms
The template "Windows Forms Application" was created successfully.
Processing post-creation actions...
Running 'dotnet restore' on C:\DevProjects\winformscore3\winformscore3.csproj...
Persisting no-op dg to C:\DevProjects\winformscore3\obj\winformscore3.csproj.nuget.dgspec.json
Restore completed in 113.9 ms for C:\DevProjects\winformscore3\winformscore3.csproj.
Restore succeeded.
PM>
https://dotnet.microsoft.com/download/dotnet-core/3.0
"To use .NET Core 3.0 with Visual Studio, you'll need Visual Studio 2019 Preview."

Convert a project.json to a csproj without using Visual Studio

As of preview3 dotnet new produces a csproj, and both dotnet restore and dotnet build fail against a project.json and/or an xproj.
For instance, running dotnet restore against a directory that has both a project.json and an xproj gives this error.
...xproj(7,3): error MSB4019: The imported project "C:\Program Files\dotnet\sdk\1.0.0-preview3-004056\Extensions\Microsoft\VisualStudio\v14.0\DotNet\Microsoft.DotNet.Props" was not found. Confirm that the path in the declaration is correct, and that the file exists on disk.
Further, running it against a directory that has only a project.json gives this error.
MSBUILD : error MSB1003: Specify a project or solution file. The current working directory does not contain a project or solution file.
Without using Visual Studio, how can we convert an existing project.json and/or xproj to a csproj?
There is a dotnet migrate command.
The dotnet migrate command will migrate a valid Preview 2 project.json based project to a valid Preview 3 csproj project.
We can find detailed help on this command by running dotnet migrate -h (even though dotnet -h does not list the command).

Resources