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.
Look at the old answer. It describes that one can use dotnet ef dbcontext scaffold with multiple -t parameters, which specifies the tables which need be scaffolded.
In Entity Framework Core, the DbSet represents the set of entities. In a database, a group of similar entities is called an Entity Set. The DbSet enables the user to perform various operations like add, remove, update, etc. on the entity set.
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. We shall try covering the below aspects overall in this article, Install EFCore scaffolding Tools.
The EF core only supports Code First & Database First approach. In Database First, We use the Scaffold-dbcontext to create the Model from an existing database. This is basically Reverse engineering the existing database. Once we create the entity classes databases first does not work.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With