我知道这不是使用单数类名的标准。但问题是,我们有许多微服务正在使用旧版本的实体框架,并且内部决定的标准是对脚手架的自动生成代码使用复数名称。
我实际上正在开发一个 .NET 6 项目,其中包括 EF Core 6.0.6,我需要以复数形式的 DB First 方法生成这些类:
我有:
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; }
...
我需要什么:
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; }
...
如您所见,唯一的区别是类名末尾的“s”。
这是我正在运行的脚手架命令:
Scaffold-DbContext -项目“Knowously.Delivery.Data”-StartupProject“Knowously.Delivery.Data”“服务器=。;数据库=交付-db;Trusted_Connection = True;集成安全性= true;MultipleActiveResultSets = true;” Microsoft.EntityFrameworkCore.SqlServer -Context DeliveryDbContext -ContextDir 。 -OutputDir 实体 -Force
我阅读了多篇文章,但没有一篇对我有帮助。有些人建议实现一个 pluralizer 服务,其他人提到了一个包 Bricelam.EntityFrameworkCore.Pluralizer 但它不起作用或者我不明白如何正确使用它。
我该怎么做?
难以置信但却是事实。
阅读了Scaffold-DbContext的文档后,我发现有一个参数说:
-NoPluralize --- 不要使用复数。 EF Core 5.0 中添加。
我决定用我的脚手架命令尝试一下:
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
瞧!有效。现在我的实体名称是复数。
奇怪的是,听起来这个参数应该做相反的事情,所以我有点困惑。不过,这解决了问题。