Reputation: 6262
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 classes 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?
Upvotes: 3
Views: 2668
Reputation: 6262
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.
Upvotes: 2