Closed Rick-Anderson closed 6 years ago
Related to the issue outlined, the scaffolder also generates singular names for collection properties on page model classes, which is odd, in my opinion. So instead of
public IList<Student> Students { get; set; }
you get
public IList<Student> Student { get; set; }
And in the Razor Page:
@foreach (var item in Model.Student) {
@prafullbhosale what is the reason to patch this? Is it just a bug, or is it really a new feature?
@Eilon sorry for the verbose write up. The scaffolder generates incorrect code when the DbSet
is plural.
public DbSet<Student> Students { get; set; }
Ah ok so it's a genuine, old-fashioned, plain & simple bug then?
Talked to @mlorbetske about this. It's indeed worth fixing, but given the timelines, we are moving this issue to the March 2018 patch.
Yes. Most folks like to use plural for the DbSet, but doing so generates invalid code. See Scaffold the model
Build the project. The build generates errors like the following:
1>Pages\Students\Index.cshtml.cs(26,38,26,45): error CS1061: 'SchoolContext' does not contain a definition for 'Student'
Globally change _context.Student
to _context.Students
(that is, add an "s" to Student). 7 occurrences are found and updated. We hope to fix this bug in the next release.
Ah ok so it's a genuine, old-fashioned, plain & simple bug then?
Yes this was a bug.
Talked to @mlorbetske about this. It's indeed worth fixing, but given the timelines, we are moving this issue to the March 2018 patch.
The fix for this is already in the placeholder branch for 2.0.x #667
@prafullbhosale please discuss with @mlorbetske whether you two agree it is safe to take for this patch. I'm fine either way.
@Eilon I talked this over with @prafullbhosale and reviewed the changes. It looks like this is low risk & could be taken for this release.
Okie dokie, approved for 2.0.6.
how do you fix this error from happening, what and where would i change the text?, having a hard time understanding what this means Globally change _context.Student to _context.Students (that is, add an "s" to Student).
i get this error ble to create an object of type 'SchoolContext'. Add an implementation of 'IDesignTimeDbContextFactory<SchoolContext
@philysworld
This issue fixes the problem and has nothing directly to do with my tutorial.
Globally means in every file where _context.Student
occurs. If you're using Visual Studio:
i just used the mvc example seems to be easier
Thank you Rick, your tutorials are the best I could find. The last clarification on how to proceed was what I needed. Some of us need as much detail as possible to fully understand the issue and how to fix it.
@Eilon - you mentioned earlier that this was targeted for the March patch. Can you comment on whether or not that happened?
@mwhitis According to https://github.com/aspnet/Scaffolding/pull/676, yes ... it was fixed. This issue (#633) was fixed on https://github.com/aspnet/Scaffolding/pull/667.
new shorter instructions
RazorPagesMovie
DbSet
pluralpublic DbSet<Movie> Movies { get; set; }
This tutorial had 30K confirming it works until an update was made to change to the plural
public DbSet<Movie> Movies { get; set; }
-- what follows is essentially the same instructions
In VS, create a new Razor Pages (RP) app named ContosoUniversity Create a Models folder. In the Models folder, create a class file named Student.cs and replace the code with the following:
In the Data folder create a new class file named SchoolContext.cs, and replace the template code with the following code:
Add SchoolContext to the DI container:
Open the appsettings.json file and add a connection string as shown in the following code:
In the Package Manager Console (PMC), enter the following commands:
*Scaffold the model
dotnet aspnet-codegenerator razorpage -m Student -dc SchoolContext -udl -outDir Pages\CU --referenceScriptLibraries
Build the project. You get errors
error CS1061: 'SchoolContext' does not contain a definition for 'Student' and no extension method 'Student'
Here is the generated code:
_context.Student.Add(Student);
It should be plural_context.Students.Add(Student);