ASP.NET Core Scaffolding does not work in VS 2017 - visual-studio

I have a beginner ASP.NET Core project in Visual Studio 2017, and I am at the scaffolding step of the HelloWorld. The Scaffolding does not work, I tested on a first computer, then at a second one...
when I try to generate a controller with views
, it gives the following error on the first machine:
Microsoft Visual Studio
Error
There was an error running the selected code generator:
'Unhandled Exception: System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.VisualStudio.Web.CodeGeneration.Utils, Version=1.1.1.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'. The system cannot find the file specified.
at Microsoft.VisualStudio.Web.CodeGeneration.Design.Program.Main(String[] args)'
OK
On the second machine:
Microsoft Visual Studio
Error
There was an error running the selected code generator:
'Error: assembly specified in the dependencies manifest was not found -- package: 'microsoft.applicationinsights.aspnetcore', version: '2.1.0', path: 'lib/netstandard1.6/Microsoft.ApplicationInsights.AspNetCore.dll''
OK

The following works for me.
I deleted bin & obj folders, build the project again and it works.
In case if it gives you error: it couldn't find project.deps.json in debug folder.
Set project to debug, ran it and now you can add scaffolded items.

Clear local NuGet cache with the command from console:
dotnet nuget locals all --clear
Restore all Packages
Install (or update) NuGet package Microsoft.CodeAnalysis.CSharp.Workspaces version 2.0.0 (or higher) EDIT: It seams versions 2.3.0 and 2.3.1 cause some problems. So stick with 2.2.0 at most until MS solves this issues (https://github.com/dotnet/roslyn/issues/20873).
Restore all Packages again and try scaffolding.
That worked for me.
Be sure you have properly referenced these packages in your project:
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="1.1.1" />
<DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="1.0.1" />
My projects uses Asp.Net Core 1.1.2 and VS2017 Community

I has the same situation. On .net core version 2.2 and with VS 2017 Enterprise.
The next instruction hepls for me:
open VS with Admin rights.
load solution to VS
add controller from scaffolding menu. VS will install 'Microsoft.VisualStudio.Web.CodeGeneration.Design' and 'Microsoft.VisualStudio.Web.CodeGeneration.Tools' packages.
close VS
open VS and load project. Add controllers via scaffloading is working fine
Maybe this instruction helps somebody,
Thanks.

For the benefit of searchers:
I got this in VS2019. The cause was that my code was broken.
Didn't realise why to start, but when you realise that code generation is a nuget package; if the project doesn't build, then Visual Studio can't get to that package.
Once I got the code compiling, this error went away.

you should change the version of Microsoft.VisualStudio.Web.CodeGeneration.Design exactly like the version of Microsoft.AspNetCore.App

On menu --> Compile --> Clean Solution
On menu --> Compile --> Build Solution.
Then repeat de creation of crud using scaffolding.
That works for me. Thanks

I was trying to run the command bellow with the version 3.0.0 of asp.net core installed, so I got the same error
Command: dotnet aspnet-codegenerator controller -name MoviesController -m Movie -dc MvcMovieContext --relativeFolderPath Controllers --useDefaultLayout --referenceScriptLibraries
Error:
Scaffolding failed.
Could not load file or assembly 'Microsoft.VisualStudio.Web.CodeGeneration, Version=3.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'. The system cannot find the file specified.
Solution:
dotnet nuget locals all --clear
dotnet remove package Microsoft.VisualStudio.Web.CodeGeneration.Utils
Change the PackageRefere for the version 2.2.0
Run the command again.
dotnet aspnet-codegenerator controller -name MoviesController -m Movie -dc MvcMovieContext --relativeFolderPath Controllers --useDefaultLayout --referenceScriptLibraries

Solution:
Go to VS2019 --> Dependencies -->Packages
Click on Manage NuGet
In the search type "Microsoft.VisualStudio.Web.CodeGeneration.Utils"
or the Dll that is missing.
Click Install.
Rebuild the solution
Then add the Scaffolding item.
It works..:-)

I fixed this by uninstalling all entity framework packages using nuget package manager and again reinstall with same version.
Microsoft.EntityFrameworkCore
Microsoft.EntityFrameworkCore.Design
Microsoft.EntityFrameworkCore.SqlServer
(3.10 version)

go to solution explorer -> clean solution then build solution
this worked for me.

I was receiving different error messages. Actually there was nothing specific, no mention of dependencies or assemblies. I tried all of the above suggestions with no success. Then I tried letting it create a new DbContext instead of specifying an existing one. That resolved my issue.

Related

Assets file project.assets.json doesn't have a target for 'net6.0' - VS2022

Just a few days ago got this error, after updating to Visual Studio Community 2022 v17.2 (from v17.1.6):
Error NETSDK1005
Assets file 'C:.........XXXXXX.Web\obj\project.assets.json' doesn't have a target for 'net6.0'. Ensure that restore has run and that you have included 'net6.0' in the TargetFrameworks for your project. XXXXXX.Web C:\Program Files\dotnet\sdk\6.0.300\Sdks\Microsoft.NET.Sdk\targets\Microsoft.PackageDependencyResolution.targets 267
Uninstalled everything related with VS2022 + Installer
Rebooted
Fresh Git Cloned the project I'm working on (I work on several computers all with Win 10 and all with the latest updates, this is the only VS installation that presents this problem)
Reinstalled VS 2022 v17.2 (with .NET 6.0, the usual install)
The .csproj file has everything in place:
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<RuntimeIdentifiers>win-x86;win-x64</RuntimeIdentifiers>
<IsPackable>false</IsPackable>
<SpaRoot>ClientApp\</SpaRoot>
<DefaultItemExcludes>$(DefaultItemExcludes);(SpaRoot)\**\node_modules\**;</DefaultItemExcludes>
<AspNetCoreHostingModel>OutOfProcess</AspNetCoreHostingModel>
</PropertyGroup>
Always delete the 'bin' and 'obj' folders before build or rebuild....
Cannot get away with the compilation, and always receiving that NETSDK1005 error...
Getting desperate :(
Thanks in advance for any help
P.S. - already checked question 70711153
I found the problem and it indeed had to do with restoring NuGet Packages, in that I have a connection to a corporate NuGet repository, and the call to it was breaking due to wrong credentials.
What was troubling was that the error did not identify the nature of problem with the connection or the username of the credentials getting refused.
On the logged in user popup dialog window, where the several used usernames are presented, there was one username that was required to re-enter its password.
That was all it took.
Visual Studio > Tools > Options > Azure Service Authentication. ReBuild and the NuGet Packages will be restored and build successful.
We had this issue in our Azure DevOps pipeline and it ended up being that the "NuGet Restore" task was using an old version of NuGet. You can see which version the pipeline is using if you check the logs for the "NuGet Restore" task and look for the "Detected NuGet" line.
We:
added in the "NuGet Tool Installer" task before the "NuGet Restore" task
Under the "Version of NuGet.exe to install", list the version you want to use, or the minimum version (e.g. >=6.1.0)
(this step is possibly overkill) Under the "NuGet Restore" Task, check "Disable local cache"
This happens because NuGet writes a file named project.assets.json in the obj\ folder & the .NET SDK uses it to get information about packages to pass into the compiler. In .NET 5, Nuget added a new field called TargetFrameworkAlias, and thus in MSBuild versions < 16.8 or NuGet versions < 5.8, it is possible that you can generate an assets file without the TargetFrameworkAlias as it will read the property and not find it.
You can resolve this issue by ensuring you are on MSBuild version 16.8+ & using NuGet version 5.8+.
In my case I have commented out the TargetAlias line and it published successfully.
Reference: https://developercommunity.visualstudio.com/t/error-netsdk1005-assets-file-projectassetsjson-doe/1248649
For me, I was getting this when updating my projects from .NET Core 3.1 to .NET 6. I had my .NET 6 code in another Git branch and when I switched from the main 3.1 branch to the 6 branch and then tried to build the solution, I would get that message.
After some trial and error, the solution that worked for me was doing the Git checkout and the restore via command line.
Close the solution
From the Developer Powershell (or using regular Powershell or the Visual Studio 2022 Command Prompt), navigate to the local directory that has your repo, and then:
git checkout [branch name]
dotnet restore
Then back in Visual Studio, reopen the solution and build, which would work.
For me, this happened after switching from .net6.0 to .net 7.0 in asp.net core / blazor project. The error occured when trying to publish the project to IIS.
Solution was to switch the "target framework" in the publishing configuration (.pubxml) in the "Publish"-tab.
For me this fix worked:
If you don't have the dotnet cmd line tool, download and install the .NET 6 SDK.
Open a cmd prompt and run the command:
dotnet restore <path to your solution>
(for instance: dotnet restore c:\app\myapp.sln)
nuget restore resolved the same issue
and/or dotnet restore
I had this bug in a solution with several SDK plus non-SDK C# projects.
What fixed my case:
Close the solution.
Separately open the first project of the solution that Visual Studio failed to build.
Build the project. --> "Error not found and build is OK"
Reopen solution. --> "Error disappeared"
I got the same error when publishing Web API to the cloud.
Use Tools ->Command line -> Developer command prompt in Visual Studio 2022, enter AZ login, and after login, restart the visual studio, it is working for me again.
Had the same problem in Azure Devops, using a Windows 2019 build server with VisualStudio 2022
Error:
##[error]C:\Program Files\dotnet\sdk\6.0.301\Sdks\Microsoft.NET.Sdk\targets\Microsoft.PackageDependencyResolution.targets(267,5): Error NETSDK1005: Assets file 'C:\agent\vso_work\4\s<..>.API\obj\project.assets.json' doesn't have a target for 'net6.0'. Ensure that restore has run and that you have included 'net6.0' in the TargetFrameworks for your project.
Resolved by adding a "NuGet Tool Installer" using version >=6.0.0
Before the NuGet restore task
enter image description here
we got this problem when added cache on gitlab, and started to use dotnet restore,
it happend because we missed the "runtime" parameter to the restore command,
- dotnet restore --packages .nuget --runtime win-x64
- dotnet publish --no-restore --runtime win-x64
In my case the problem was I had updated the Nuget package version in one assembly but not in another, so check you have the same nuget package versions across your solution.
For me, it works to set the target framework to another framework like .NET Core 3.1, build the application, set the framework to the original framework and rebuild.
In my case I had inadvertently added a couple of projects to the solution that were in another folder. I received no errors until I changed NET version from NET6.0 to NET6.0-windows on one of the projects. The solution then didn't build, with 100s of errors, but each individual project built OK.
I noticed that "project.assets.json' doesn't have a target" error among all the errors, pointing to the outside folder.
Bringing those projects into the solution folder fixed the error.
I got the same error sometime back. This worked for me: Logout from visual studio and login to visual studio account
I had an error:
Ensure that restore has run and that you have included 'net6.0' in the
TargetFrameworks for your project. You may also need to include
'win10-x64' in your project's RuntimeIdentifiers.
I removed bin and obj folders for this project and rebuilt the project. dotnet clean, dotnet restore didn't work for me.
I had the same problem ("...\obj\project.assets.json' doesn't have a target for 'net6.0'. Ensure that restore...") with clean batch compilation of my sln: msbuild 17.4, nuget 4.7.
I replaced string
nuget.exe restore my.sln
with string
msbuild.exe my.sln /t:Restore
that was before
msbuild.exe my.sln /t:Build
and everything worked.
I got the same error this morning.
This worked for me: right click on solution explorer in visual studio -> 'Restore NuGet Packages'.
Hope this helps.

ASP.NET CORE 2.1 Preview "SharedCompilationId" parameter is not supported by the "Csc" task

I am trying run the project with views in separate class library on Mac OSX as described in below article
https://blogs.msdn.microsoft.com/webdev/2018/03/01/asp-net-core-2-1-razor-ui-in-class-libraries/
The problem is that I cannot build the class library project with Visual Studio Community 7.4 on Mac OS.
Project builds with command line (donet build) without problems but when I try to build it with Visual Studio it throws two errors:
/usr/local/share/dotnet/sdk/NuGetFallbackFolder/microsoft.aspnetcore.razor.design/2.1.0-preview1-final/build/netstandard2.0/Microsoft.AspNetCore.Razor.Design.Compilation.targets(10,10): Error MSB4064: The "SharedCompilationId" parameter is not supported by the "Csc" task. Verify the parameter exists on the task, and it is a settable public instance property. (MSB4064)
/usr/local/share/dotnet/sdk/NuGetFallbackFolder/microsoft.aspnetcore.razor.design/2.1.0-preview1-final/build/netstandard2.0/Microsoft.AspNetCore.Razor.Design.Compilation.targets(5,5): Error MSB4063: The "Csc" task could not be initialized with its input parameters. (MSB4063)
When I change RazorCompileOnBuild to false or remove Content from Content Build project compiles without errors.
Go to Nuget Console and add:
Install-Package Microsoft.Net.Compilers -Version 2.8.2
You will no longer get above mentioned error.
I had this problem on the build server but not locally.
The solution:
Install the updated Visual Studio on the build server. In my case 2017.15.4
As I find you can solve this issue in two ways.
Solution 1
Add Microsoft.Net.Compilers compilers package to the project.
Solution 2
Build the project using the command line. dotnet build
In my case, I had installed the .NET Core 2.1 SDK, but not the runtime.
Strange how Microsoft don't include the runtime in the SDK.
If you face this problem there are three steps you need to take:
Ensure that Nuget package and Target Framework match.
Ensure that the referenced runtime and sdk are installed.
Ensure that Visual Studio is up to date.

Cannot install Microsoft.NETCore.UniversalWindowsPlatform

Seemed to be related to this:
Nuget error install package Microsoft.NETCore.UniversalWindowsPlatform
but it is not, because the checkbox is checked by default in vs2017. I am running the creators update by the way and everything is up-to-date.
I have just started a new C# background IoT project and got this:
When manually installing the package in the package console I got this:
...
Successfully installed 'Microsoft.Net.Native.Compiler 1.6.0' to projectnamehere
Install failed. Rolling back...
Package 'Microsoft.NETCore.Jit.1.0.3' does not exist in project
...
Package 'Microsoft.NETCore.Jit.1.0.3' does not exist in folder
...
Install-Package : Could not install package 'Microsoft.NETCore.Jit 1.0.3'. You are trying to install this package into a project that targets '.NETCore,Version=v5.0', but the package does not contain any assembly references or content files that are compatible with that f
ramework. For more information, contact the package author.
seems related to this https://connect.microsoft.com/VisualStudio/feedback/details/1617801/newly-created-universal-app-projects-dont-work
but I have no idea how to fix it.
I have also seen a website suggesting to remove the project.json file, which I did but did not resolve it.
I have also closed vs2017 and modified the installtion to verify that the creators update sdk was checked and installed.
I have lowered the target version in the project props, but that did not help either.
edit manual install the jit package did not work either:
Could not install package 'Microsoft.NETCore.Jit 1.1.1'. You are trying to install this package into a project that targets '.NETCore,Version=v5.0', but the package does not contain any assembly references or content files that are compatible with that framework. For more information, contact the package author.
edit2 funny thing is that the solution explorer tells me that there is a project.json while there is none in windows explorer:
I verified the sdk installation by hitting the modify button of the vs2017 installation and tried to create a new project afterwards but got the exact same issue again.
Update: Microsoft updated the templates in their github (https://github.com/ms-iot/samples/commit/2e2aa34ab514b8c0725a53263898a412e0a1be1c) but didn't push it as an updated templates package to Visual Studio yet. Changed the answer accordingly.
Temporary fix:
replace in your .csproj file
<ItemGroup>
<!-- A reference to the entire .Net Framework and Windows SDK are automatically included -->
<None Include="project.json" />
</ItemGroup>
with
<PropertyGroup>
<RestoreProjectStyle>PackageReference</RestoreProjectStyle>
</PropertyGroup>
add the Microsoft.NETCore.UniversalWindowsPlatform package
Optional extra:
Add the Windows IoT Extension SDK via the references dialog
This issue has been fixed for Visual Studio 2017 in updated templates released here: https://marketplace.visualstudio.com/items?itemName=MicrosoftIoT.WindowsIoTCoreProjectTemplatesforVS15

How can I install and use Entity Framework Core in Visual Studio Code?

I have already created the initial asp.net mvc web application template in visual studio code.
And I can run the application in a browser at localhost:5000/
I can also see the views, controllers, viewmodels like the previous asp.net mvc core.
Now, how can I install and use entity framework core using visual studio code?
Thanks.
VS Code is just an editor. It doesn't install EF (or any packages). You can install EF by editing your csproj file to contain this set of lines.
<ItemGroup>
<PackageGroup Include="Microsoft.EntityFrameworkCore.SqlServer" Version="1.1.0" />
</ItemGroup>
VS Code may give you a prompt to "restore" pacakges. If not, call dotnet restore on command line.
Alternatively, on command line you can execute:
dotnet add package Microsoft.EntityFrameworkCore.SqlServer
See the "Getting Started" guide for more details on EF. To use EF, you need to write code. A full example is beyond the scope of a good StackOverflow answer and is subject to change as EF Core continually updates.
1st :
You need three line of code to install EntityFrameWork core and its dependencies.
2nd :
install them in this form
dotnet add package Microsoft.EntityFrameworkCore -v 2.1.14
dotnet add package Microsoft.EntityFrameworkCore.tools -v 2.1.14
dotnet add package Microsoft.EntityFrameworkCore.SqlServer -v 2.1.14
I put a sample of them with version but you can get latest entityframework version if you remove version in last of line.
like this.
dotnet add package Microsoft.EntityFrameworkCore
dotnet add package Microsoft.EntityFrameworkCore.tools
dotnet add package Microsoft.EntityFrameworkCore.SqlServer
3rd
Remember your .Ner Core version should be compatible with lastest version of EntityFrameWork core which you want to install.So if you have error either you have to install lastet version of .Net Core or install EntityFrameWork core with a compatible version with .Net Core.
Probably you are looking for EntityFrameWork core version here and also for .net Core version here
To be able to use entity framework in visual code you can use yeoman on the terminal:
>npm install -g yo
>npm install -g generator-efrepo
>yo efrepo
Then follow the instructions
In addition to answers given above, following article has all the steps for adding EF Core using .Net Core CLI.
Getting Started with EF Core
Hope this helps to someone.
*Edit:
- VS Code users can execute dotnet commands from Terminal window (Ctrl+Shift+`)
I think this is the updated link, as of June 3.
https://docs.efproject.net/en/latest/
In my case i had deleted the "csproj" by accident, after research a lot, i knew about this possibility and found the deleted file in my trash.

Nuget versioning issue with package restore

I am unable to install a package (using package restore) due to some kind of versioning issue with Nuget. Here are the steps I took:
build project:
error : The schema version of 'MagicalUnicorn.MvcErrorToolkit' is incompatible with version 1.6.21205.9031 of NuGet. Please upgrade NuGet to the latest version from http://go.microsoft.com/fwlink/?LinkId=213942.
Extension manager says version is: 2.0.30619.9119.
Ok, restart VS as administrator, extension manager uninstall nuget.
restart VS as administrator again, search for nuget and install.
Extension manager again says version is: 2.0.30619.9119.
rebuild project:
error : The schema version of 'MagicalUnicorn.MvcErrorToolkit' is incompatible with version 1.6.21205.9031 of NuGet. Please upgrade NuGet to the latest version from http://go.microsoft.com/fwlink/?LinkId=213942.
Ok, so restart VS as administrator, extension manager uninstall nuget again.
This time download from http://visualstudiogallery.msdn.microsoft.com/27077b70-9dad-4c64-adcf-c7cf6bc9970c
Run the installer, all seems ok.
Extension manager again says version is: 2.0.30619.9119.
rebuild project:
error : The schema version of 'MagicalUnicorn.MvcErrorToolkit' is incompatible with version 1.6.21205.9031 of NuGet. Please upgrade NuGet to the latest version from http://go.microsoft.com/fwlink/?LinkId=213942.
Grr! Any ideas?
It looks like problem in nuget command line tool. When you enable 'Nuget package restore' feature it adds .nuget folder to you solution with nuget.exe. And when you compile project this tool is using to download missing packages, not VS extension is used here. So try update this tool from command line:
cd .nuget
nuget.exe update -Self
Procedure screenshot:
The following worked for me in Visual Studio 2012:
Run VS 2012 as Administrator
Click Tools -- Extensions and Updates
In the left nav, Updates -- Visual Studio Gallery
Click Update on NuGet Package Manager
Install the update
Restart Visual Studio
None of the offered solutions worked for me (Visual Studio 2013, Nuget Package Manager Version 2.8.5).
I had same error but it was saying I had version 2.5.4.
I fixed it by going to Tools > Nuget Manager Console > Nuget Package Console. This open's up a command line window. At the top of the window was a prompt to download missing packages for my project.
Pressed the download button and errors went away upon build.
I was facing the same issue. I resolved it by selecting Tools on VS 2010 Menu --> Library Package Manager --> Package Manager Settings --> Check "Automatically check for updates". After that I restarted VS and was prompted for Nuget Update Installation. Once the update was installed, the Nuget package, HTTP Client in my case, got installed smoothly.
I faced to this problem and I checked all answers that were this page and at the end my problem did not solved. After lot of search on web I could update my nuget from original site
http://docs.nuget.org/docs/start-here/installing-nuget
and for example for vs2010 :
http://visualstudiogallery.msdn.microsoft.com/27077b70-9dad-4c64-adcf-c7cf6bc9970c
At this site select your version of VS and then update it .
Note : before installing nuget,close all Visual Studios and after install,Open your project.
Thanks
This above did not work for me, completely. There is one last step that is IMPORTANT to note. After downloading the latest nuget version, you may notice that the same error prints in the output window. The reason is due to the .nuget folder containing a NuGet.exe which specifies a version number.
The solution (for me and perhaps to those of your that did not resolve your issue with the above directions), is to
Delete the .nuget folder from you solution.
Right click your web project and click Enable Nuget Restore.
The directions above should cause a NEW .nuget folder to be added, this time the NuGet.exe should be the newest version of nuget that you downloaded.

Resources