Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
TraySimpson opened this issue Oct 2, 2023 · 2 comments

Comments

@TraySimpson
Copy link

TraySimpson commented Oct 2, 2023

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
Copy link
Contributor

will take a look soon tyty

@AlizerUncaged
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants