scaffolder doesn't use plural from DbSet
See original GitHub issuenew shorter instructions
- Create a RP web app named
RazorPagesMovie
- Follow these instructions EXCEPT - name the
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:
using System;
using System.Collections.Generic;
namespace ContosoUniversity.Models
{
public class Student
{
public int ID { get; set; }
public string LastName { get; set; }
public string FirstMidName { get; set; }
public DateTime EnrollmentDate { get; set; }
}
}
In the Data folder create a new class file named SchoolContext.cs, and replace the template code with the following code:
using ContosoUniversity.Models;
using Microsoft.EntityFrameworkCore;
namespace ContosoUniversity.Data
{
public class SchoolContext : DbContext
{
public SchoolContext(DbContextOptions<SchoolContext> options) : base(options)
{
}
public DbSet<Student> Students { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Student>().ToTable("Student");
}
}
}
Add SchoolContext to the DI container:
// ADD EF using
// using Microsoft.EntityFrameworkCore;
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<Data.SchoolContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
services.AddMvc();
}
Open the appsettings.json file and add a connection string as shown in the following code:
{
"ConnectionStrings": {
"DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=ContosoUniversity1;Trusted_Connection=True;MultipleActiveResultSets=true"
},
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Warning"
}
}
}
In the Package Manager Console (PMC), enter the following commands:
Install-Package Microsoft.VisualStudio.Web.CodeGeneration.Design -Version 2.0.0
Add-Migration Initial
Update-Database
*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);
Issue Analytics
- State:
- Created 6 years ago
- Comments:16 (10 by maintainers)
Top GitHub Comments
@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.
i just used the mvc example seems to be easier