Create a new Web API, don’t select “use controllers” (using controllers probably has the same issues)
Add a model type using a primary constructor:
public class Book(int id, string title, string? publisher = default)
{
public int Id { get; set; } = id;
public string Title { get; set; } = title;
public string? Publisher { get; set; } = publisher;
}
Add | New Scaffolded Item
Select Common, API --> API with read/write endpoints, using Entity Framework
Click Add
Select the model class Book
Click on the + with the Endpoints class, click Add
Click the + with the DbContext class, click Add
Click Add to finish the dialog
This results in an exception, error creating the DbContext, CS1514, expects { instead of the primary constructor.
As refactoring methods are available to use this new constructor, I expect this constructor to work with scaffolding.
Using Visual Studio, scaffolding API endpoints with read/write endpoints, using EF Core fails when using primary constructors using C# 12.
I've already reported this with the Visual Studio Developer Community, but I think this GitHub repo is a better place: https://developercommunity.visualstudio.com/t/NET-8:-scaffolding-API-endpoints-with-r/10505296
Versions: .NET: 8.0.100-rc.2.23502.2 Visual Studio: 17.8.0 Preview 6.0
This results in an exception, error creating the DbContext, CS1514, expects
{
instead of the primary constructor.As refactoring methods are available to use this new constructor, I expect this constructor to work with scaffolding.
See the project https://github.com/christiannagel/issues-scaffoldingapi/tree/main/PrimaryConstructor for a simple test.
Using an EF Core context with a primary constructor
When adding an EF Core context with a primary constructor, another error occurs:
See this project as a start with EF Core primary constructor: https://github.com/christiannagel/issues-scaffoldingapi/tree/main/PrimaryConstructor2
Again, I would expect this to work with primary constructors.