Hi,
I created a new webapi application with dotnet cli and I'm trying to scaffolding with dotnet aspnet-codegenerator but it's failing.
Here's the error:
Command Line: --project . controller -name PacientesController -m Paciente -outDir Controllers/
Microsoft (R) Build Engine version 15.1.548.43366
Copyright (C) Microsoft Corporation. All rights reserved.
/mnt/docs/dev/acs/acs.csproj : error MSB4057: The target "EvaluateProjectInfoForCodeGeneration" does not exist in the project.
Unhandled Exception: System.InvalidOperationException: Failed to get Project Context for /mnt/docs/dev/acs/acs.csproj.
at Microsoft.Extensions.ProjectModel.MsBuildProjectContextBuilder.Build()
at Microsoft.VisualStudio.Web.CodeGeneration.Tools.Program.GetProjectInformation(String projectPath, String configuration)
at Microsoft.VisualStudio.Web.CodeGeneration.Tools.Program.BuildAndDispatchDependencyCommand(String[] args, String projectPath, String buildBasePath, String configuration, Boolean noBuild, ILogger logger)
at Microsoft.Extensions.CommandLineUtils.CommandLineApplication.Execute(String[] args)
at Microsoft.VisualStudio.Web.CodeGeneration.Tools.Program.Main(String[] args)
my .csproj
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp1.1.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Folder Include="wwwroot\" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore" Version="1.1.1" />
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="1.1.2" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="1.1.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="1.1.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="1.1.1" PrivateAssets="All" />
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="1.1.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="1.1.0" PrivateAssets="All" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="1.1.0" PrivateAssets="All" />
<PackageReference Include="Microsoft.VisualStudio.Web.BrowserLink" Version="1.1.0" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Msbuild" Version="1.1.0" />
</ItemGroup>
<ItemGroup>
<DotNetCliToolReference Include="Microsoft.DotNet.Watcher.Tools" Version="1.0.0" />
<DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="1.0.0" />
<DotNetCliToolReference Include="Microsoft.Extensions.SecretManager.Tools" Version="1.0.0" />
<DotNetCliToolReference Include="Microsoft.Extensions.Caching.SqlConfig.Tools" Version="1.0.0" />
<DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="1.0.0" />
</ItemGroup>
</Project>
dotnet --info
.NET Command Line Tools (1.0.1)
Product Information:
Version: 1.0.1
Commit SHA-1 hash: 005db40cd1
Runtime Environment:
OS Name: opensuse
OS Version: 20170331
OS Platform: Linux
RID: opensuse.13.2-x64
Base Path: /opt/dotnet/sdk/1.0.1
This also occurs on OSX
.NET Command Line Tools (1.0.1)
Product Information:
Version: 1.0.1
Commit SHA-1 hash: 005db40cd1
Runtime Environment:
OS Name: Mac OS X
OS Version: 10.12
OS Platform: Darwin
RID: osx.10.12-x64
Base Path: /usr/local/share/dotnet/sdk/1.0.1
$ dotnet aspnet-codegenerator controller -name BlogController -m Post -dc BlogApplicationContext
Command Line: controller -name BlogController -m Post -dc BlogApplicationContext
Microsoft (R) Build Engine version 15.1.548.43366
Copyright (C) Microsoft Corporation. All rights reserved.
/Users/***/Documents/Development/ASP/BlogApplication/BlogApplication.csproj : error MSB4057: The target "EvaluateProjectInfoForCodeGeneration" does not exist in the project.
Unhandled Exception: System.InvalidOperationException: Failed to get Project Context for /Users/***/Documents/Development/ASP/BlogApplication/BlogApplication.csproj.
at Microsoft.Extensions.ProjectModel.MsBuildProjectContextBuilder.Build()
at Microsoft.VisualStudio.Web.CodeGeneration.Tools.Program.GetProjectInformation(String projectPath, String configuration)
at Microsoft.VisualStudio.Web.CodeGeneration.Tools.Program.BuildAndDispatchDependencyCommand(String[] args, String projectPath, String buildBasePath, String configuration, Boolean noBuild, ILogger logger)
at Microsoft.Extensions.CommandLineUtils.CommandLineApplication.Execute(String[] args)
at Microsoft.VisualStudio.Web.CodeGeneration.Tools.Program.Main(String[] args)
@thild
Can you add the below line to your csproj and try if that works?
<PropertyGroup>
<PackageTargetFallback>portable-net45+win8</TargetFramework>
</PropertyGroup>
Also, you do not need to add the below to your project.
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Msbuild" Version="1.1.0" />
@prafullbhosale
I had already tried that. Same error.
I also tried this PackageTargetFallback:
<PackageTargetFallback>$(PackageTargetFallback);dotnet5.6;portable-net45+win8</PackageTargetFallback>
@thild, I have found the issue. I will create a PR for the fix.
In the meanwhile a workaround you can try is as below:
Locate *.codegeneration.targets under your project_directory/obj/
Replace the below
<ImportGroup Condition=" '$(TargetFramework)' == '' AND '$(ExcludeRestorePackageImports)' != 'true' ">
<Import Project="$(CodeGenerationTargetLocation)\buildMultiTargeting\microsoft.visualstudio.web.codegeneration.tools.targets" Condition="Exists('$(CodeGenerationTargetLocation)\buildCrossTargeting\microsoft.visualstudio.web.codegeneration.tools.targets')" />
</ImportGroup>
<ImportGroup Condition=" '$(TargetFramework)' != '' AND '$(ExcludeRestorePackageImports)' != 'true' ">
<Import Project="$(CodeGenerationTargetLocation)\build\netstandard1.0\microsoft.visualstudio.web.codegeneration.tools.targets" Condition="Exists('$(CodeGenerationTargetLocation)\build\netstandard1.0\microsoft.visualstudio.web.codegeneration.tools.targets')" />
</ImportGroup>
with
<ImportGroup Condition=" '$(TargetFramework)' == '' AND '$(ExcludeRestorePackageImports)' != 'true' ">
<Import Project="$(CodeGenerationTargetLocation)\buildMultiTargeting\Microsoft.VisualStudio.Web.CodeGeneration.Tools.targets" Condition="Exists('$(CodeGenerationTargetLocation)\buildCrossTargeting\Microsoft.VisualStudio.Web.CodeGeneration.Tools.targets')" />
</ImportGroup>
<ImportGroup Condition=" '$(TargetFramework)' != '' AND '$(ExcludeRestorePackageImports)' != 'true' ">
<Import Project="$(CodeGenerationTargetLocation)\build\netstandard1.0\Microsoft.VisualStudio.Web.CodeGeneration.Tools.targets" Condition="Exists('$(CodeGenerationTargetLocation)\build\netstandard1.0\Microsoft.VisualStudio.Web.CodeGeneration.Tools.targets')" />
</ImportGroup>
Note this file gets generated when run scaffolding the first time on a project.
cc @mlorbetske @Eilon
@prafullbhosale I can confirm your fix works on OSX :) Thanks very much
@prafullbhosale Your fix works on Linux too. Just a casing issue? Thanks.
i got this exception when i tried to scaffold a controller in vs2017.
@yoyoys me too.
Closing this as the fix is available in version 1.0.1 of Microsoft.VisualStudio.Web.CodeGeneration.Tools
You can update the version in the .csproj file
<DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="1.0.1" />
Feel free to reopen this if you still see the issue after upgrading.
I just encountered this today on VS 2017 (15.2 26430.6). I tried applying the workaround and updating the code generation tool version, but I'm still seeing it happen when trying to create a templated Edit view for a view model. I've closed and reopened the solution, as well as VS. Anything I'm missing?

@llaughlin have you tried clearing the obj folder ?
Could you also share the contents of the .csproj file?
@prafullbhosale I'm also hitting same issues as @llaughlin with VS2017.
I practically followed this guide. Only exception is that I've EF Models and Context in separate project.
Late response, but I've been busy working around the issue.
@prafullbhosale Yes, I've deleted the /bin and /obj folders, re-opening visual studio, and I still get this error. Here are the contents of my .csproj file:
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net452</TargetFramework>
<PreserveCompilationContext>true</PreserveCompilationContext>
<AssemblyName>PLX.AutoloadMonitor.Web</AssemblyName>
<OutputType>Exe</OutputType>
<PackageId>PLX.AutoloadMonitor.Web</PackageId>
<UserSecretsId>aspnet-PLX.AutoloadMonitor.Web-871b58f8-19b8-405d-8b38-563af113700e</UserSecretsId>
<Authors>PLX.AutoloadMonitor.Web</Authors>
<Company>PLX.AutoloadMonitor.Web</Company>
<Product>PLX.AutoloadMonitor.Web</Product>
<RootNamespace>PLX.AutoloadMonitor.Web</RootNamespace>
</PropertyGroup>
<ItemGroup>
<Compile Remove="wwwroot\css\**" />
<Content Remove="wwwroot\css\**" />
<EmbeddedResource Remove="wwwroot\css\**" />
<None Remove="wwwroot\css\**" />
</ItemGroup>
<ItemGroup>
<Content Include="wwwroot\css\site.css" />
<Content Include="wwwroot\css\site.min.css" />
<Content Include="wwwroot\favicon.ico" />
<Content Include="wwwroot\images\BAr.PNG" />
<Content Include="wwwroot\images\donut.PNG" />
<Content Include="wwwroot\images\Graph.PNG" />
<Content Include="wwwroot\less\site.less" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="EntityFramework" Version="6.1.3" />
<PackageReference Include="ExpressionToCodeLib" Version="2.4.0" />
<PackageReference Include="Expressmapper" Version="1.9.1" />
<PackageReference Include="Microsoft.AspNet.Identity.Core" Version="2.2.1" />
<PackageReference Include="Microsoft.AspNetCore" Version="1.0.5" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.Cookies" Version="1.1.2" />
<PackageReference Include="Microsoft.AspNetCore.Diagnostics" Version="1.1.2" />
<PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="1.1.2" />
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="1.1.2" />
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="1.1.3" />
<PackageReference Include="Microsoft.AspNetCore.Routing" Version="1.1.2" />
<PackageReference Include="Microsoft.AspNetCore.Server.IISIntegration" Version="1.1.2" />
<PackageReference Include="Microsoft.AspNetCore.Server.Kestrel" Version="1.1.2" />
<PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="1.1.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="1.1.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="1.1.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer.Design" Version="1.1.2">
<PrivateAssets>All</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="1.1.1">
<PrivateAssets>All</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="1.1.2" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="1.1.2" />
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="1.1.2" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="1.1.2" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="1.1.2" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="1.1.2" />
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="1.1.2" />
<PackageReference Include="Microsoft.Extensions.Primitives" Version="1.1.1" />
<PackageReference Include="Microsoft.VisualStudio.Web.BrowserLink" Version="1.1.2" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="1.1.1">
<PrivateAssets>All</PrivateAssets>
</PackageReference>
<PackageReference Include="Serilog.Extensions.Logging" Version="1.4.0" />
<PackageReference Include="Serilog.Sinks.Seq" Version="3.3.1" />
<PackageReference Include="StackifyMiddleware" Version="2.0.3" />
<PackageReference Include="System.ValueTuple" Version="4.3.1" />
</ItemGroup>
<ItemGroup Condition=" '$(TargetFramework)' == 'net461' ">
<Reference Include="System" />
<Reference Include="Microsoft.CSharp" />
</ItemGroup>
<Target Name="PrepublishScript" BeforeTargets="PrepareForPublish">
<Exec Command="bower install" />
<Exec Command="dotnet bundle" />
</Target>
<ItemGroup>
<DotNetCliToolReference Include="BundlerMinifier.Core" Version="2.2.301" />
<DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="1.0.0" />
<DotNetCliToolReference Include="Microsoft.Extensions.SecretManager.Tools" Version="1.0.0" />
<DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="1.0.1" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\PLX.AutoloadMonitor.Accessors\PLX.AutoloadMonitor.Accessors.csproj" />
<ProjectReference Include="..\PLX.AutoloadMonitor.Contracts\PLX.AutoloadMonitor.Contracts.csproj" />
<ProjectReference Include="..\PLX.AutoloadMonitor.Managers\PLX.AutoloadMonitor.Managers.csproj" />
<ProjectReference Include="..\SerilogAdapters\SerilogAdapters.csproj" />
</ItemGroup>
<ItemGroup>
<Folder Include="Views\Shared\DisplayTemplates\" />
</ItemGroup>
</Project>
@llaughlin @mholo65 and others still hitting this problem. Can you reproduce this with a new project? Just add a model and scaffold.
I too was getting this error when trying to scaffold a Controller in my WebAPI project.
I am targeting .NET 4.7 and have a mix of F# and C# projects.
My C# WebAPI project is of type "ASP.NET Core Web Application (.NET Framework).
I have project references to the F# projects. F# is targeting .NET Framework 4.7 and F# runtime 4.4.1.0.
So when I removed the references to the F# projects it worked fine. But this is a workaround. I shouldn't have to remove references every time I need to add a controller. I am on Windows 10 / VS2017Pro Version 15.2 (26430.16).
I have the same problem that llaughlin reported 2nd June. I just installed VS build 26430.16. I have both 4.52 and Core in my solution. Does anyone have any further info or suggestions?
@PeterMontgomery777 you installed the 4.52 Framework and VS on Linux?
Sorry, no, it was on Windows 7!
FWIW, I also had this issue with an upgraded project on Windows 10 which was resolved by deleting obj folder as directed by prafullbhosale.
The interesting thing was that the scaffolding worked fine using VS 2017 (version 15.3). For the command line, I had to add the following to my csproj and delete obj to get it working.
<DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.0.0" />
I had to delete everything in obj and debug, restored the packages again and then this issue was resolved for me.
Most helpful comment
@thild, I have found the issue. I will create a PR for the fix.
In the meanwhile a workaround you can try is as below:
Locate
*.codegeneration.targetsunder yourproject_directory/obj/Replace the below
with
Note this file gets generated when run scaffolding the first time on a project.