You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Running the application fails with an InvalidOperationException:
InvalidOperationException: Could not create an instance of type 'RazorPagesWithEFCore.Models.Book'. Model bound complex types must not be abstract or value types and must have a parameterless constructor. Record types must have a single primary constructor. Alternatively, set the 'Book' property to a non-null value in the 'RazorPagesWithEFCore.CreateModel' constructor.
Describe the solution you'd like
Using a parameterless constructor as described with the error, the application runs. I would prefer to use the primary constructor. Using records, the error mentions it's ok using a primary constructor. This should be possible with a class as well (and scaffolding creates code successfully).
Additional context
The alternative option mentioned in the error message "Alternatively, set the 'Book' property to a non-null value in the 'RazorPagesWithEFCore.CreateModel' constructor.", is not working. Assigning a non-null value to the Book property fails as well.
The text was updated successfully, but these errors were encountered:
christiannagel
changed the title
BindProperty fails with primary constructors with classes
BindProperty should support primary constructors with classes
Oct 2, 2024
christiannagel
changed the title
BindProperty should support primary constructors with classes
BindProperty attribute should support primary constructors with classes
Oct 2, 2024
Is there an existing issue for this?
Is your feature request related to a problem? Please describe the problem.
In an ASP.NET Core Razor project, creating this class with a primary constructor
succeeds in scaffolding creating Razor pages with EF Core.
This is the code from the Create page code-behind:
Running the application fails with an
InvalidOperationException
:Describe the solution you'd like
Using a parameterless constructor as described with the error, the application runs. I would prefer to use the primary constructor. Using records, the error mentions it's ok using a primary constructor. This should be possible with a class as well (and scaffolding creates code successfully).
Additional context
The alternative option mentioned in the error message "Alternatively, set the 'Book' property to a non-null value in the 'RazorPagesWithEFCore.CreateModel' constructor.", is not working. Assigning a non-null value to the
Book
property fails as well.The text was updated successfully, but these errors were encountered: