我使用最新的 EFCore 版本和 Postgres 数据库,采用 DB First 方法。从现有 Postgres 数据库生成 Dbcontext 时,它会从表名和列名中消除下划线(“_”)。
我使用以下命令来生成 DbContext:
Scaffold-DbContext "Server=localhost;Port=5432;User Id=postgres;Password=mypassword;Database=SAMPLE_db;" Npgsql.EntityFrameworkCore.PostgreSQL -o DbModels -Schemas "local_dev"
正如 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