Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent Entity Framework Core 2.0 from Renaming Tables and Columns in Generated Classes (Database First)

I have been working on an ASP.NET MVC Core 2.0 project (targeting .NET Core 2.0) that needs to make use of Entity Framework. The database to be used already exists and I can't change it. I have added all the required references to Entity Framework Core 2.0.

However, when I run the following scaffolding command from the Nuget Package Manager Console, I get POCO model classes for the tables with different capitalization as compared to the original tables. In addition to other differences, columns with underlines in their names are represented by properties without the underlines.

How can I force the scaffolding to leave all table and column names as is when the POCO model classes are generated?

Example of scaffold command. Scaffold-DbContext "Server= Info;Database=Vehicles;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models –force

(First time posting to Stack Overflow. Thanks for your help.)

like image 592
MikeDev Avatar asked Oct 25 '17 14:10

MikeDev


People also ask

Is Scaffold DbContext command is used in EF Code First approach?

Entity Framework Core supports Database-First approach via the Scaffold-DbContext command of Package Manager Console. This command scaffolds a DbContext and entity type classes for a specified database.

How do I update Entity Framework model from database first in .NET core?

Updating the Model If you need to re-scaffold the model after database schema changes have been made, you can do so by specifying the -f or --force option e.g.: dotnet ef dbcontext scaffold "Server=. \;Database=AdventureWorksLT2012;Trusted_Connection=True;" Microsoft. EntityFrameworkCore.


1 Answers

The -UseDatabaseNames option is intended to preserve the names exactly as they are in the database (with only minor corrections if they aren't valid C# identifiers).

Unfortunately, this option got left off the Scaffold-DbContext command in version 2.0 (issue #9146) and it wasn't applied to property names (issue #9820). You'll have to wait for EF Core 2.1 to use it as it was intended. If you're eager to try it sooner, you can always use the nightly builds.

like image 163
bricelam Avatar answered Jun 20 '23 15:06

bricelam