Reputation: 25039
UPDATE 2023: for modern EF Core versions (.NET 5+), pluralization is enabled by default, as it is shown in the answer https://stackoverflow.com/a/64801660/5112433. The rest of the discussion, while totally relevant at the time given, is nothing more than a history.
Original question:
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.
Upvotes: 25
Views: 22296
Reputation: 1
I may be late to the party, but after an upgrade from .net core 3.1 to .net 6, when I used the following command: dbcontext scaffold "Server=(local);Database=XXXXXX;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -o Models\Entities -c DBContext -f --verbose --no-onconfiguring --startup-project ../XXXX^ it duplicated my entities by generating the singluar form of them. Up until the upgrade de command was giving me the name from the database of the entities so it was not adding the duplicated singular form. I found that using the --no-pluralize option kept the command from generating another entity with singular form. I know they added a default pluralizer in net core 5, but I have no ideea why this worked in preventing the singularization. So Cris Loli's aswer seems to solve my issue. Hope this helps someone solve their issues.
Upvotes: 0
Reputation: 71
You can use this command :
Scaffold-DbContext "connectionString" Microsoft.EntityFrameworkCore.SqlServer -o Models -UseDatabaseNames -NoPluralize -force
You have nuget package before:
Microsoft.EntityFrameworkCore.Design - version 5
Microsoft.EntityFrameworkCore - version 5
Microsoft.EntityFrameworkCore.SqlServer -version 5
Microsoft.EntityFrameworkCore.Tools - version 5
Microsoft.EntityFrameworkCore.Relational -version 5
Upvotes: 7
Reputation: 3711
Update for EntityFrameworkCore 5.0 : TableNames are now automatically pluralized upon scaffolding. (At least with -Provider Microsoft.EntityFrameworkCore.SqlServer)
Upvotes: 5
Reputation: 1529
Install-Package Bricelam.EntityFrameworkCore.Pluralizer
Scaffold-DbContext
CommandScaffold-DbContext -Connection "Server=<server>;Database=<dbname>;user id=<userid>;password=<pwd>;" -Provider Microsoft.EntityFrameworkCore.SqlServer -OutputDir Data/EFModels/
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
Upvotes: 8
Reputation: 25039
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.
Upvotes: 2
Reputation: 26823
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
Upvotes: 1