dotnet / Scaffolding

Code generators to speed up development.
MIT License
627 stars 225 forks source link

`Could not get the reflection type for DbContext` when using `required` keyword in model #2546

Open TraySimpson opened 10 months ago

TraySimpson commented 10 months ago

I was following along with this API tutorial when I came upon an odd exception. It took a bit of guess-and-check testing, but I narrowed it down to only occur when the required keyword is added (below is the example model).

The tutorial lists the .NET 7.0 SDK in the prerequisites. From what I've found in the documentation, that should use C# 11 (which is when the required keyword was introduced). Is this an issue with the aspnet-codegenerator tool, the documentation, or my own misunderstanding?

Code:

namespace TodoApi.Models;

public class TodoItem
{
    public long Id { get; set; }
    public required string? Name { get; set; }
    public bool IsComplete { get; set; }
}

Stack Trace:

$ dotnet aspnet-codegenerator controller -name TodoItemsController -async -api -m TodoItem -dc TodoContext -outDir Controllers
Building project ...
Finding the generator 'controller'...
Running the generator 'controller'...

Minimal hosting scenario!
Attempting to compile the application in memory.
Could not get the reflection type for DbContext : TodoApi.Models.TodoContext
   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)
RunTime 00:00:09.75

Include provider and version information

Microsoft.VisualStudio.Web.CodeGeneration.Design version: 7.0.0 Target framework: .NET 7.0 Operating system: Windows 11 IDE: VS Code 1.82.2

deepchoudhery commented 10 months ago

will take a look soon tyty

AlizerUncaged commented 7 months ago

This is an issue even now, I'm still getting a lot of errors if I use scaffolding with the required keyword

tm01013 commented 1 month ago

Same Issue... I can't scaffold : (