question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

scaffolder doesn't use plural from DbSet

See original GitHub issue

new shorter instructions

  1. Create a RP web app named RazorPagesMovie
  2. Follow these instructions EXCEPT - name the DbSet plural public 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:closed
  • Created 6 years ago
  • Comments:16 (10 by maintainers)

github_iconTop GitHub Comments

1reaction
guardrexcommented, Apr 18, 2018

@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.

1reaction
philysworldcommented, Feb 16, 2018

i just used the mvc example seems to be easier

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to scaffold DbContext with plural DbSet property ...
Short Answer. 1. Install Package. Install-Package Bricelam.EntityFrameworkCore.Pluralizer. 2. Run Scaffold-DbContext Command.
Read more >
Scaffolding Wizard - The plural form of entity names is ...
Our Scaffolding Wizard obtains a plural form for collections using a corresponding DbContext. All you need is to add a corresponding DbSet ......
Read more >
Pluralization with EF Core and EF Core Power Tools
EF Core 3 supports pluralization by convention, and allows you to enable and customize it. This blog post will show you how!
Read more >
Breaking changes in EF Core 5.0
To disable the pluralizer, use the --no-pluralize option on dotnet ef dbcontext scaffold or the -NoPluralize switch on Scaffold-DbContext .
Read more >
Razor Pages with Entity Framework Core in ASP.NET Core
There are 8 occurrences. Because an entity set contains multiple entities, many developers prefer the DBSet property names should be plural. ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found