How to scaffold DbContext with plural DbSet property names in Entity Framework Core?

I use Scaffold-DbContext command in Package Manager Console to create and re-create context and entities for an existed SQL Server database:

Scaffold-DbContext -provider EntityFramework.MicrosoftSqlServer -connection "my connection string"

It works perfectly except one thing: DbSet's have property names in singular form:

public partial class MyDbContext : DbContext
{
    public virtual DbSet<Request> Request { get; set; }
    public virtual DbSet<RequestHeader> RequestHeader { get; set; }
}

I prefer these names to be in plural form (Requests etc.). In addition to web search I checked command syntax:

get-Help Scaffold-DbContext -detailed

And found nothing to change this behaviour. Here is my packages.config:

<packages>
  <package id="EntityFramework.Commands" version="7.0.0-rc1-final" targetFramework="net46" />
  <package id="EntityFramework.Core" version="7.0.0-rc1-final" targetFramework="net46" />
  ...
</packages>

How to pluralize DbSet names when scaffolding?

UPDATE 2017-04: DB First scaffolding pluralization is now possible in Entity Framework Core 1.1. Read my answer below for details.

Short Answer
1. Install Package

Install-Package Bricelam.EntityFrameworkCore.Pluralizer

2. Run Scaffold-DbContext Command

Scaffold-DbContext -Connection "Server=<server>;Database=<dbname>;user id=<userid>;password=<pwd>;" -Provider Microsoft.EntityFrameworkCore.SqlServer -OutputDir Data/EFModels/


Long Answer:

As pointed out by @KalinKrastev in the comments of @natemcmaster's answer. Pluralization in EF Core is possible using a package called Bricelam.EntityFrameworkCore.Pluralizer that can be installed using

in the Package Manager Console (PMC) or

dotnet add package Bricelam.EntityFrameworkCore.Pluralizer

using Dotnet cli.

After installing the package just use the regular Scaffold-DbContext command.

Scaffold-DbContext -Connection "Server=<server>;Database=<dbname>;user id=<userid>;password=<pwd>;" -Provider Microsoft.EntityFrameworkCore.SqlServer -OutputDir Data/EFModels/ -Force

See More About Bricelam's Pluralizer

How do i scaffold DbContext with plural DbSet property names in as , If you are using Visual Studio, I recommend you use the following extension EF Core Power Tools. Among its features is generating code from� I use Scaffold-DbContext command in Package Manager Console to create and re-create context and entities for an existed SQL Server database: Scaffold-DbContext -provider EntityFramework.MicrosoftSqlServer -connection "my connection string" It works perfectly except one thing: DbSet's have property names in singular form:

Pluralization is possible in EF Core 1.1. As Rowan Miller described in its blog, you need to install the Inflector and implement IDesignTimeServices to control pluralization when scaffolding. However, be aware of it:

We put these services into *.internal namespaces and reserve the right to break the APIs at any point.

So this is why a full code example is not copied to here. This answer is not accepted for the same reason - I prefer to wait until we get a stable API.

Another issue you should consider - this solution is provider-dependent. It works fine with SQL Server (I tested it). Another DBMS provider may not support this API yet. For example, latest Npgsql.EntityFrameworkCore.PostgreSQL 1.1.0 fails on scaffold when custom IDesignTimeServices is used.

Pluralization with EF Core and EF Core Power Tools, EF Core 3 supports pluralization by convention, and allows you to enable will by convention get pluralized DbSet and navigation property names: If you have plural table names in your legacy database, their entity class If you are using the EF Core console command dotnet ef dbcontext scaffold or� How do i scaffold DbContext with plural DbSet property names in asp.net core EFCore 3 ? and where they exist more than one capital letters in a table name (e.g. AutoShipment) it generates all except the first letters lowercase (e.g. Autoshipment.cs)

Pluralization is not supported in EF7 as of RC1. This and other limitations of EF7 scaffolding are being tracked here: https://github.com/aspnet/EntityFramework/issues/4038

Pluralization in EF Core, We've been improving design-time extensibility in EF Core 2.1. One new feature is Pluralizer dotnet ef dbcontext scaffold "Filename=chinook.db" Microsoft. After installing it, you'll get pluralized DbSet property names… In this article, we shall see step by step details on various useful Scaffold-DbContext commands of EFCore. Scaffold-DbContext commands help scaffolding entity type classes and a DbContext class based on a database schema thereby automating the code generation technique related to database access.

Scaffold-dbcontext: Cannot combine -UseDataBaseNames with , How to scaffold DbContext with plural DbSet property names in Entity Framework Core? Question. I use Scaffold-DbContext command in Package Manager� We’ve been improving design-time extensibility in EF Core 2.1. One new feature is the ability for NuGet packages to register design-time services. As a reference implementation for how to do this, I’ve created the bricelam\\EFCore.Pluralizer repo. While the repo primarily serves as a sample for anyone who wants to create design-time extensions for EF Core, anyone that uses EF Core with an

Will Scaffold-DbContext -UseDatabaseNames use singular class , scaffold-dbcontext by default generates a DBSet property named XytableName Scaffolding with EFCore 3 for custom field names and Plurals bricelam/EFCore. Enable UseDatabasenames with Pluralization for EF Core 5� In .net core 2.0.5 Scaffold-DbContext -UseDatabaseNames is not working as noted, but it is supposed to be restored in .net core 2.1.: #9146. I want to know if class names will be generated that are singular i.e. if the table is named Forks will the class be named Fork. Will the DbSet names in the generated context use the names of the tables?

EntityFramework Core database first approach pluralizing table names, We have existing code from EF 6 using a database first approach. In .net core 2.0.5 Scaffold-DbContext -UseDatabaseNames is not working as Will the DbSet names in the generated context use the names of the tables? Use the following command to get the detailed help on Scaffold-DbContext command: PM> get-help scaffold-dbcontext –detailed. The above Scaffold-DbContext command creates entity classes for each table in the SchoolDB database and context class (by deriving DbContext) with Fluent API configurations for all the entities in the Models folder.

Comments
  • read this and this
  • Doctor - this is no longer supported, but something like this will appear after RTM
  • See my answer here: stackoverflow.com/a/47410837/869033
  • @NickN. The IDesignTimeServices trick is known since EF Core 1.1. See my answer here =) stackoverflow.com/a/43320867/5112433
  • The IPluralizer interface is new though, it's way easier now.
  • Finally, it works for different providers. I scaffolded a PostgreSQL db with the only change in the command: -Provider Npgsql.EntityFrameworkCore.PostgreSQL
  • Bricelam's solution works great. Works great! What is still unsolved for me though is, that where they exist more than one capital letters in a table name (e.g. GPSSettings) it generates all except the first letters lowercase (e.g. Gpssettings.cs)
  • In case anybody is wondering about what is Bricelam's solution discussed in the @KalinKrastev's comment above, he can look at bricelam.net/2018/03/02/efcore-pluralization.html