2

I know this is not the standard that is to use singular class names. But the thing is that we have many microservices which are using an old version of Entity Framework and the standard decided internally is to use plural names for auto-generated code of the Scaffolding.

I'm actually working on a .NET 6 project which includes EF Core 6.0.6 and I need to generate those cloases on a DB First approach to be plural:

What I have:

public partial class DeliveryDbContext : DbContext
{
    public DeliveryDbContext()
    {
    }

    public DeliveryDbContext(DbContextOptions<DeliveryDbContext> options)
        : base(options)
    {
    }

    public virtual DbSet<Deliverable> Deliverables { get; set; }
    public virtual DbSet<DeliverableDeliveryMethod> DeliverableDeliveryMethods { get; set; }
 ...

What I need:

public partial class DeliveryDbContext : DbContext
{
    public DeliveryDbContext()
    {
    }

    public DeliveryDbContext(DbContextOptions<DeliveryDbContext> options)
        : base(options)
    {
    }

    public virtual DbSet<Deliverables> Deliverables { get; set; }
    public virtual DbSet<DeliverableDeliveryMethods> DeliverableDeliveryMethods { get; set; }
 ...

As you can see the only difference is the trailing "s" at the at of the class name.

This is the scaffold command that I'm running:

Scaffold-DbContext -Project "Knowfully.Delivery.Data" -StartupProject "Knowfully.Delivery.Data" "Server=.;Database=delivery-db;Trusted_Connection=True;Integrated Security=true;MultipleActiveResultSets=true;" Microsoft.EntityFrameworkCore.SqlServer -Context DeliveryDbContext -ContextDir . -OutputDir Entities -Force

I have read multiple posts and none of them helped me out. Some suggest to implement a pluralizer service and others mentioned a package Bricelam.EntityFrameworkCore.Pluralizer but it didn't work or I don't understand how to use it properly.

How can I do this?

halfer
  • 19,824
  • 17
  • 99
  • 186
Mauro Bilotti
  • 5,628
  • 4
  • 44
  • 65
  • Not sure if I understood you correctly, but If you have code that need to change names - open you project in VSCode and do a whole project search for YourClassName and replace it with YourClassNames. – user19139505 Jun 15 '22 at 18:06
  • What makes you think the `Bricelam.EntityFrameworkCore.Pluralizer` package doesn't work? It's explicitly there to achieve what you want. Other than pluralising the class names which you absolutely should NOT be doing. – DavidG Jun 15 '22 at 18:09
  • That is not a possible solution if you're planning to build a microservice with 100 entities on it. That's why I wanted to run the scaffold command to generate those as plural by default. – Mauro Bilotti Jun 15 '22 at 18:09
  • 1
    @DavidG, basicly I don't see any change after installing it. The library says that's just enough to install it a should make the magic, but is not working on my end. Did you use it before? – Mauro Bilotti Jun 15 '22 at 18:13
  • That package didn't work for me either, it might be because of a [breaking change the underlying Microsoft.EntityFrameworkCore.Design library](https://learn.microsoft.com/en-us/ef/core/what-is-new/ef-core-3.x/breaking-changes#microsoftentityframeworkcoredesign-is-now-a-developmentdependency-package) in EF Core 3.1. At least it was for [others](https://github.com/dotnet/efcore/issues/20258) on related [issues](https://github.com/dotnet/EntityFramework.Docs/issues/2178#issuecomment-597918347) that were trying to do something similar to the `Bricelam.EntityFrameworkCore.Pluralizer` package. – John Cummings Apr 14 '23 at 16:27

1 Answers1

2

Incredible but true.

After reading the documentation for the Scaffold-DbContext, I have found that there's a parameter that says:

-NoPluralize --- Don't use the pluralizer. Added in EF Core 5.0.

I decided to give it a try with my scaffolding command this way:

PM> Scaffold-DbContext -Project "MyProject.Data" -StartupProject "MyProject.Data" "Server=.;Database=myDB;Trusted_Connection=True;Integrated Security=true;MultipleActiveResultSets=true;" -Provider Microsoft.EntityFrameworkCore.SqlServer -Context MyDbContext -ContextDir . -OutputDir Entities -Force -NoPluralize

And voila! It worked. Now my entities are plural in name.

The weird thing is that sounds like the parameter suppose to do the opposite, so I'm kind of confused. However, this solves the issue.

Mauro Bilotti
  • 5,628
  • 4
  • 44
  • 65
  • 1
    For those using the CLI, the option is [`--no-pluralize`](https://learn.microsoft.com/en-us/ef/core/cli/dotnet#dotnet-ef-dbcontext-scaffold) – John Cummings Apr 14 '23 at 16:23