如何首先使用数据库在 EF Core 中自定义实体名称大小写

How to customise entity name casing in EF Core with database first

我有一个现有的 EF6 代码库,我正在尝试将其移植到 EF Core,并且我有一个我不能 erase/modify 的现有数据库。所以我正在使用以下命令从我的数据库

中构建实体 classes
Scaffold-DbContext "MyConnectionString" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models

它确实会生成 DbContext 和必要的实体。现在,他们似乎已经改变了很多命名约定(如何命名实体、属性和导航),而且新创建的实体与我现有的实体有很大不同!我找不到自定义这些约定的方法,所以我不得不在很多地方手动更改代码! (伤心!)

其中一项更改是:现在 EF 不会将 table 名称中的 class 名称单数化。例如如果 table 名字是 'Accessories',之前的实体 class 曾经是 'Accessory',现在是 'Accessories'!

为了解决这个问题,我使用了 IPluralizer interface and used the Humanizer 库来单独化实体 class 名称。

当我使用 Humanizer 库时,它会执行此操作

"Accessories".Singularize(); //produces "Accessory"
"XMLDetails".Singularize(); //produces "XMLDetail"

但是,在 EF Core 创建的最终实体 class 中,第二个 属性 名称变成了 Xmldetail,忽略了原来的大小写!有没有办法通过EF Core脚手架的过程来保留原来的外壳?

注意:我正在使用 Microsoft.EntityFrameworkCore.SqlServer 版本 2.0.1 和 class 库目标 .NET Standard 2.0.

我不确定。

One of the changes is: Now EF does not singularize class names from table name. e.g. if table name is 'Accessories', earlier entity class used to be 'Accessory', now it is 'Accessories'!

据我所知,连接 EF 到 DB 时有一个选项。它的输出与早期版本相同。

像这样:https://dotnetinterviewquestion.files.wordpress.com/2013/10/ut.png

经过一些研究,我从 GitHub 社区了解到,这是回购协议中的一个 known issue in EF Core 2.0 and it has already been fixed此修复计划与 EF Core 2.1 一起发布。

修复发布后,您可以使用 --use-database-names 标志告诉 EF 完全根据数据库名称生成名称,如下所示

Scaffold-DbContext "ConnString" Microsoft.EntityFrameworkCore.SqlServer -o Models --use-database-names

在此之前,您可以尝试使用以下工具之一,它提供 more/better 比默认 EF Core 工具更多的选项(我还没有实际使用过它们)

  1. EF Core Power Tools
  2. EntityFramework-Reverse-POCO-Code-First-Generator