'Scaffold-DbContext: Underscores eliminates while generating Context from postgres Database in .NetCore

I am using the latest EFCore version with the Postgres database using the DB First approach. While generating Dbcontext from an existing Postgres database it eliminates underscore ('_') from the table name and also from the column names.

Database table structure

Generated DbContext model structure

I am using the following command to generate DbContext:

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



Solution 1:[1]

As Ivan Stove mentioned above, use the dbcontext scaffold --use-database-names argument. This will remove things like underscores from both table names and fields.

Here's an example using the terminal:
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

Notes
-o Models: The folder in your solution you want the model files to be written to. It needs to already exist.
-t Individual table: This is handy is you only want a subset of tables to have models generated. Just repeat the "-t" for each table

I hope this helps somebody as it was driving me crazy.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Rich Ward