11
votes

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.)

2
I strongly suggest that you use the names being generated. They follow C# recommended standardsDavidG
Wouldn't that mean I would need to rename the tables and their columns on my database to match the names generated in the POCO classes? Otherwise I believe my DbContext SaveChanges() would fail.MikeDev
@DavidG Problem is that some of my tables have the letter "s" on the end of their names. The table name is not in plural, for example "bas". But EF creates a model "ba" removing the trailing s. In this case it has nothing to do with convetnion. The old EF for .Net framework had the optio to leave all the names alone and create model 1:1. (I know its an old post but it may lead to knew answers on this problem)Sed
OK , i solved my problem with -NoPluralize option. Altough it doesnt sound like the solution for my problem as i expected something that is called "no singularize" since i didtn want it to not to put my word into sigular form.Sed

2 Answers

13
votes

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.

5
votes

Solution: You have to update to .Net Core 2.1+, then you can run Scaffold-DbContext -UseDatabaseNames. I have tried this in Net 2.1 RC, it's working now.