Trying to use aspnet-codegenerator to scaffold a razor page as outlined here;
https://docs.microsoft.com/en-us/aspnet/core/tutorials/razor-pages/model
And getting an "Unhandled Exception: System.InvalidOperationException: Failed to get Project Context" for any action. My project is targeting net47
>dotnet aspnet-codegenerator razorpage -h
Unhandled Exception: System.InvalidOperationException: Failed to get Project Context for C:\Dev\Neptune\Neptune.Web\Neptune.Web.csproj.
at Microsoft.Extensions.ProjectModel.MsBuildProjectContextBuilder.Build()
at Microsoft.VisualStudio.Web.CodeGeneration.Tools.Program.<>c__DisplayClass8_0.<Execute>b__0()
at Microsoft.Extensions.CommandLineUtils.CommandLineApplication.Execute(String[] args)
at Microsoft.VisualStudio.Web.CodeGeneration.Tools.Program.Execute(String[] args, Boolean isNoBuild, ILogger logger)
at Microsoft.VisualStudio.Web.CodeGeneration.Tools.Program.Main(String[] args)
My .csproj looks like the following
<PropertyGroup>
<TargetFramework>net47</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore" Version="2.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.Cookies" Version="2.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="2.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="2.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.ViewCompilation" Version="2.0.0" PrivateAssets="All" />
<PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="2.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="2.0.0" PrivateAssets="All" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.0.0" />
<PackageReference Include="Microsoft.VisualStudio.Web.BrowserLink" Version="2.0.0" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.0.0" PrivateAssets="All" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Templating" Version="2.0.0" />
</ItemGroup>
<ItemGroup>
<DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.0" />
<DotNetCliToolReference Include="Microsoft.Extensions.SecretManager.Tools" Version="2.0.0" />
<DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.0.0" />
</ItemGroup>
The problem is that dotnet build fails (see https://github.com/dotnet/cli/issues/8187). So other tools like the Entity Framework ones that return the contexts in the project don't run. This is also why Apply Migrations checkbox does not appear in Visual Studio Web Publish Settings.
Yep, same problem. Fixed build and problem went away.
Closing since fixing the build fixes the issue.
Would it be possible to have the errors point to common causes like this?
Like people above are saying, after I ran dotnet build, it worked for me as well 馃憤馃榿馃憤
Most helpful comment
The problem is that dotnet build fails (see https://github.com/dotnet/cli/issues/8187). So other tools like the Entity Framework ones that return the contexts in the project don't run. This is also why Apply Migrations checkbox does not appear in Visual Studio Web Publish Settings.