4

I am using EF Core 3.0 with a database-first approach to Scaffold-DbContext from a SQL Server database:

dotnet ef dbcontext 
       scaffold "Server=***;Database=***;User Id=***;Password=****;" Microsoft.EntityFrameworkCore.SqlServer 
       -o Models -c "MyContext" --project=Data

When the models are generated, it uses a specified naming for the variable for example:

if the column in the database called for example

  • ISBuy becomes Isbuy
  • CategoryID => CategoryId

and this really misses up my system ...

Is there is a way to prevent that converting and generate variable name with the same as database or is there is a super magical way to prevent just these and use camel case as EF Core does?

And I am not talking about the object that returned from the API because I found a lot of solutions for that case but never this one.

Thanks

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Hassan Ahmed
  • 73
  • 4
  • 10

2 Answers2

6

In PowerShell, you can use the UseDatabaseNames option (with a single dash) as part of a Scaffold-DbContext call to keep the casing used in the database schema.

Scaffold-DbContext "Server=.\SERVERNAME;Database=DBNAME;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir ENTITIESDIR -Context "NameContext" -UseDatabaseNames
ruffin
  • 16,507
  • 9
  • 88
  • 138
JOSE NAVARRO
  • 71
  • 1
  • 2
3

Add --use-database-names in your .NET Core CLI,

dotnet ef dbcontext 
   scaffold "Server=***;Database=***;User Id=***;Password=****;" Microsoft.EntityFrameworkCore.SqlServer 
   -o Models -c "MyContext" --project=Data --use-database-names

refer to

https://learn.microsoft.com/en-us/ef/core/miscellaneous/cli/dotnet#dotnet-ef-dbcontext-scaffold

--use-database-names
Use table and column names exactly as they appear in the database. If this option is omitted, database names are changed to more closely conform to C# name style conventions.

Ryan
  • 19,118
  • 10
  • 37
  • 53