Scaffold-DbContext:从 .NetCore 中的 postgres 数据库生成上下文时消除下划线

问题描述 投票:0回答:2

我使用最新的 EFCore 版本和 Postgres 数据库,采用 DB First 方法。从现有 Postgres 数据库生成 Dbcontext 时,它会从表名和列名中消除下划线(“_”)。

Database table structure

Generated DbContext model structure

我使用以下命令来生成 DbContext:

Scaffold-DbContext "Server=localhost;Port=5432;User Id=postgres;Password=mypassword;Database=SAMPLE_db;" Npgsql.EntityFrameworkCore.PostgreSQL -o DbModels -Schemas "local_dev"

postgresql entity-framework-core ef-database-first asp.net-core-scaffolding
2个回答
1
投票

正如 Ivan Stove 上面提到的,使用 dbcontext 脚手架

--use-database-names
参数。这将从表名和字段中删除下划线等内容。

这是使用终端的示例:

dotnet ef dbcontext scaffold "server=*SQL Server name*;user=*user name*;password=*password*;database=*database name*" Microsoft.EntityFrameworkCore.SqlServer -o Models -t *table name 1* -t *table name 2* -t *table name x* -f **--use-database-names**

备注:

  • -o Models
    :解决方案中您想要写入模型文件的文件夹。它需要已经存在。
  • -t Individual table
    :这很方便,因为您只希望表的子集生成模型。 只需对每张桌子重复
    -t

0
投票

此问题现已在最新版本的 EF 中修复。 使用

-UseDatabaseNames
选项

参见此处Scaffold-DbContext 未生成与表中相同的列

© www.soinside.com 2019 - 2024. All rights reserved.