Scaffolding: Scaffolding fails without details on why, when using certain c# language syntax
Environment:
- ASP.Net Core 2.1
- Unspecificed C# language in project settings, I believe I earlier had downloaded and “installed” a newer c# (7.3) version. Then removed it from the project because of then also scaffolding failing.
- Visual Studio Community 2017, Windows
This was one I really did not like guys! Since the error gives no clue about what is going on I had to backtrack my commits and guess which change broke scaffolding, thankfully i recalled that scaffolding has some internal build mechanism not using same c# version as the current project, and is therefor sensitive to certain c# language syntax, which I assumes is new c# syntax.
Finding the generator 'razorpage'...
Running the generator 'razorpage'...
Attempting to compile the application in memory.
at Microsoft.VisualStudio.Web.CodeGeneration.ActionInvoker.<BuildCommandLine>b__6_0()
at Microsoft.Extensions.CommandLineUtils.CommandLineApplication.Execute(String[] args)
at Microsoft.VisualStudio.Web.CodeGeneration.ActionInvoker.Execute(String[] args)
at Microsoft.VisualStudio.Web.CodeGeneration.CodeGenCommand.Execute(String[] args)
The problematic code was using implied default value default vs default(CancellationToken)
Does NOT work:
public async override Task<int> SaveChangesAsync(bool acceptAllChangesOnSuccess, CancellationToken cancellationToken = default)
{
...
}
Does work:
public async override Task<int> SaveChangesAsync(bool acceptAllChangesOnSuccess, CancellationToken cancellationToken = default(CancellationToken))
{
...
}
Suggested fixes/changes from a user perspective:
- (MUST) Validate, then give better error message that code failed validation according to scaffolding build mechanism.
- (MAYBE) Have scaffolding build with same c# version as project (I am on a limb here, I might have missunderstood the tech)
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Reactions: 6
- Comments: 52 (19 by maintainers)
If anyone needs a quick fix to this before the PR merge goes out into an official package, I found installing the following packages makes the scaffolding work again:
Thanks, you can fix this only with one package (its include the rest of the needed packages )
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="3.3.1" />We are using EF Core 2.2.6 and this issue is not fixed.
This is still and Issue today with .net and ef core 5.0.3 Still to this day the dialog shows up with no descriptive error message really Microsoft can this not be updated to a more mean-full message.
Any Ideas @deepchoudhery and @vijayrkn
Visual Studio Verison
I noticed here looking at my nuget reference that 5.0.3 has not been released yet could that be part of my issue.
https://www.nuget.org/packages/Microsoft.VisualStudio.Web.CodeGeneration.Design/
Here is my project include files
</ItemGroup>@Andrioden - Thanks for reporting this problem. For the short-term, I have this PR #824 in progress to provide better error messaging. For your situation, the error will look like this:
For the bigger problem of needing scaffolding to build with the same C# version as the project, we will need to look into the issue more, before deciding on a course of action.
+1 same problem here. Thanks god I found this post and just lost 30 minutes on it.
I have the same issue but I was using page.razor and code added in page.razor.cs when i move the code to page.razor the Scaffolding work Fine
i’m starting to learn this technology and following all the tutorials Microsoft provide, like:
https://docs.microsoft.com/en-us/aspnet/core/tutorials/razor-pages/model?view=aspnetcore-2.2&tabs=visual-studio
they should include the @OrihuelaConde solution to the tutorial. works like a charm for me
Quick fix doesn’t work for me sadly. It’s complaining about “using declaration” not being supported and it’s telling me to switch to language version preview. My project file is already set to preview language version in the csproj.
Also tried code analysis 3.3.0-beta1-final, exact same thing.
I guess I’ll just generate it in a new temp project and copy it over instead… Edit: Yep did that, that worked… Quite a hassle
How can this not be planned? We can only scaffold if we use c# 7 and nothing beyond? How is this a proper product decision?
Any progress on this? It is beyond annoying.
@Andrioden @InsomniumBR @seancpeters Doesn’t look like my changes really help. It just happened that I was testing against a version of our project that the stable scaffolder seems to work for as well.
However, now I actually went through and found the change in our project that caused the scaffolder to start to fail. It looks like generally C# >7.0 features work fine, but I found two patterns that seem to always cause the error discussed in this issue. Both of the examples seem to apply regardless whether they are in your entity classes or somewhere else in your codebase.
Fails
private object Test => default;Succeedsprivate object Test => null;Fails
private object Test() => new object[] { }.Max(Select);private static object Select(object o) => o;Succeedsprivate object Test() => new object[] { }.Max(o => Select(o));private static object Select(object o) => o;@Andrioden if you mean the #824, it’s not really a fix though. Or is the actually issue of the scaffolder not supporting the latest C# features being tracked somewhere as well? @seancpeters ?
#824 will be included in release/2.2