EF Core 6:运行脚手架时如何使班级名称复数

发布于 2025-02-07 13:24:20 字数 1817 浏览 3 评论 0原文

我知道这不是使用单数类名称的标准。但是事实是,我们有许多使用旧版本的实体框架的微服务,并且在内部确定的标准是使用复数名称进行脚手架的自动生成代码。

我实际上正在研究一个.NET 6项目,其中包括EF Core 6.0.6,我需要在DB的第一种方法上生成这些类,以使其复数:

我所拥有的:

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”。

这是我正在运行的脚手架命令:

ackaffold -dbcontext -project“ nowlefly.delivery.data” -startupproject“ nowly.delivery.data”“ server =。; database = .; database = delivery -delivered_connection = trumed_connection = true; intemated; intemated; intemate; intemate; intemate; intemate; intemate; intemate; intemate; intemate; Microsoft.EntityFrameWorkCore.sqlServer -Context velriveDbContext -ContextDir。 -outputdir实体-force

我阅读了多个帖子,它们都没有帮助我。有些人建议实现 pluralizer Service 其他人提到了一个软件包 bricelam.entityframeworkcore.pluralizer ,但它不起作用,或者我不明白如何正确使用它。

我该怎么做?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

土豪我们做朋友吧 2025-02-14 13:24:20

令人难以置信但真实。

阅读,我发现有一个参数说:

-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

瞧!它起作用。现在我的实体是多元的。

奇怪的是,这听起来像是应该做相反的参数,所以我有点困惑。但是,这解决了问题。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文