Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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.

like image 431
Ilya Chumakov Avatar asked Dec 31 '15 08:12

Ilya Chumakov


People also ask

Can we scaffold-DbContext from selected tables of an existing database?

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.

What is DbSet in Entity Framework Core?

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.

What is scaffold-DbContext command?

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.

Is scaffold-DbContext command is used in EF Code First approach?

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.


1 Answers

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

like image 102
Hamza Khanzada Avatar answered Sep 19 '22 10:09

Hamza Khanzada